
<!DOCTYPE HTML>
<html lang="zh-hans" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>3.ConcurrentHashMap分析 · 计算机 ,java , 大数据 基础知识体系</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        <meta name="author" content="hzh">
        
        
    
    <link rel="stylesheet" href="../../gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-splitter/splitter.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-expandable-chapters-small/expandable-chapters-small.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-anchors/plugin.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-anchor-navigation-ex/style/plugin.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-expandable-chapters/expandable-chapters.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-toggle-chapters/toggle.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-back-to-top-button/plugin.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-code/plugin.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-search-pro/search.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-pageview-count/plugin.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    

        
    
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../../gitbook/images/favicon.ico" type="image/x-icon">

    
    
    <link rel="prev" href="2.HashMap源码分析.html" />
    

    
    
    
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="输入并搜索" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        <li class="header">操作系统</li>
        
        
    
        <li class="chapter " data-level="1.1" data-path="../../">
            
                <a href="../../">
            
                    
                    Introduction
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="../../操作系统/1.基本概述.html">
            
                <a href="../../操作系统/1.基本概述.html">
            
                    
                    1.操作系统基本概述
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="../../操作系统/2.进程管理之进程与线程.html">
            
                <a href="../../操作系统/2.进程管理之进程与线程.html">
            
                    
                    2.进程管理之进程与线程
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="../../操作系统/3.进程管理之处理机调度.html">
            
                <a href="../../操作系统/3.进程管理之处理机调度.html">
            
                    
                    3.进程管理之处理机调度
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5" data-path="../../操作系统/4.进程管理之进程同步.html">
            
                <a href="../../操作系统/4.进程管理之进程同步.html">
            
                    
                    4.进程管理之进程同步
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6" data-path="../../操作系统/5.内存管理之基本概念.html">
            
                <a href="../../操作系统/5.内存管理之基本概念.html">
            
                    
                    5.内存管理之基本概念
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7" data-path="../../操作系统/6.内存管理之虚拟内存.html">
            
                <a href="../../操作系统/6.内存管理之虚拟内存.html">
            
                    
                    6.内存管理之虚拟内存
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8" data-path="../../操作系统/7.IO设备管理之基本概念.html">
            
                <a href="../../操作系统/7.IO设备管理之基本概念.html">
            
                    
                    7.IO设备管理之基本概念
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.9" data-path="../../操作系统/8.面试题总结一.html">
            
                <a href="../../操作系统/8.面试题总结一.html">
            
                    
                    8.面试题总结一
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">计算机网络篇</li>
        
        
    
        <li class="chapter " data-level="2.1" data-path="../../计算机网络/1.计算机网络入门基础概念.html">
            
                <a href="../../计算机网络/1.计算机网络入门基础概念.html">
            
                    
                    1.计算机网络入门文章(必读)
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.2" data-path="../../计算机网络/2.计算机网络通信基础.html">
            
                <a href="../../计算机网络/2.计算机网络通信基础.html">
            
                    
                    2.计算机网络通信入门文章(必读)
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.3" data-path="../../计算机网络/3.数据链路层.html">
            
                <a href="../../计算机网络/3.数据链路层.html">
            
                    
                    3.数据链路层
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.4" data-path="../../计算机网络/4.网络层1.html">
            
                <a href="../../计算机网络/4.网络层1.html">
            
                    
                    4.网络层(一)
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.5" data-path="../../计算机网络/5.网络层2.html">
            
                <a href="../../计算机网络/5.网络层2.html">
            
                    
                    5.网络层(二)
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.6" data-path="../../计算机网络/6.传输层.html">
            
                <a href="../../计算机网络/6.传输层.html">
            
                    
                    6.传输层
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.7" data-path="../../计算机网络/7.应用层.html">
            
                <a href="../../计算机网络/7.应用层.html">
            
                    
                    7.应用层(⭐)
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.8" data-path="../../计算机网络/10.HTTP相关协议.html">
            
                <a href="../../计算机网络/10.HTTP相关协议.html">
            
                    
                    8.HTTP相关及发展
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.9" >
            
                <span>
            
                    
                    9.websocket
            
                </span>
            

            
        </li>
    
        <li class="chapter " data-level="2.10" data-path="../../计算机网络/9.面试题二.html">
            
                <a href="../../计算机网络/9.面试题二.html">
            
                    
                    面试题总结一
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">数据结构篇</li>
        
        
    
        <li class="chapter " data-level="3.1" data-path="../../数据结构/1.图论.html">
            
                <a href="../../数据结构/1.图论.html">
            
                    
                    1.图论
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.2" data-path="../../数据结构/2.图论例题.html">
            
                <a href="../../数据结构/2.图论例题.html">
            
                    
                    2.图论例题
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.3" data-path="../../数据结构/3.散列表.html">
            
                <a href="../../数据结构/3.散列表.html">
            
                    
                    3.散列表
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.4" data-path="../../数据结构/4.排序算法.html">
            
                <a href="../../数据结构/4.排序算法.html">
            
                    
                    4.排序算法
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.5" data-path="../../数据结构/5.排序列题.html">
            
                <a href="../../数据结构/5.排序列题.html">
            
                    
                    5.排序算法例题
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.6" data-path="../../数据结构/6.树论.html">
            
                <a href="../../数据结构/6.树论.html">
            
                    
                    6.树论
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.7" data-path="../../数据结构/7.树论例题.html">
            
                <a href="../../数据结构/7.树论例题.html">
            
                    
                    7.树论例题.md
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.8" data-path="../../数据结构/9.模板.html">
            
                <a href="../../数据结构/9.模板.html">
            
                    
                    数据结构模板
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">算法篇</li>
        
        
    
        <li class="chapter " data-level="4.1" data-path="../../算法/leetCode周赛题解.html">
            
                <a href="../../算法/leetCode周赛题解.html">
            
                    
                    leetCode周赛记录
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.2" data-path="../../算法/1.时间复杂度.html">
            
                <a href="../../算法/1.时间复杂度.html">
            
                    
                    1.时间复杂度
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.3" data-path="../../算法/2.深度优先搜索.html">
            
                <a href="../../算法/2.深度优先搜索.html">
            
                    
                    2.深度优先搜索算法
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.4" data-path="../../算法/2.深度优先搜索题目集.html">
            
                <a href="../../算法/2.深度优先搜索题目集.html">
            
                    
                    2.深搜题目集
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.5" data-path="../../算法/3.广度优先搜索.html">
            
                <a href="../../算法/3.广度优先搜索.html">
            
                    
                    3.广度优先搜索算法
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">JVM篇</li>
        
        
    
        <li class="chapter " data-level="5.1" data-path="../../jvm/1.jvm内存结构.html">
            
                <a href="../../jvm/1.jvm内存结构.html">
            
                    
                    1.jvm内存结构
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="5.2" data-path="../../jvm/2.HotSpot虚拟机对象探秘.html">
            
                <a href="../../jvm/2.HotSpot虚拟机对象探秘.html">
            
                    
                    2.HotSpot虚拟机对象探秘.md
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="5.3" data-path="../../jvm/3.对象存亡与垃圾收集算法.html">
            
                <a href="../../jvm/3.对象存亡与垃圾收集算法.html">
            
                    
                    3.对象存亡与垃圾收集算法.md
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="5.4" data-path="../../jvm/4.HotSpot的算法细节与垃圾收集器.html">
            
                <a href="../../jvm/4.HotSpot的算法细节与垃圾收集器.html">
            
                    
                    4.HotSpot的算法细节与垃圾收集器.md
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="5.5" data-path="../../jvm/5.内存分配与回收策略.html">
            
                <a href="../../jvm/5.内存分配与回收策略.html">
            
                    
                    5.内存分配与回收策略.md
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="5.6" data-path="../../jvm/6.类文件结构.html">
            
                <a href="../../jvm/6.类文件结构.html">
            
                    
                    6.类文件结构.md
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="5.7" data-path="../../jvm/7.类加载机制.html">
            
                <a href="../../jvm/7.类加载机制.html">
            
                    
                    7.类加载机制.md
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="5.8" data-path="../../jvm/8.jvm逃逸分析.html">
            
                <a href="../../jvm/8.jvm逃逸分析.html">
            
                    
                    8.逃逸分析技术
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">多线程篇</li>
        
        
    
        <li class="chapter " data-level="6.1" data-path="../../多线程/basic/0.多线程路线图.html">
            
                <a href="../../多线程/basic/0.多线程路线图.html">
            
                    
                    1.多线程学习路线图
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="6.2" data-path="../../多线程/basic/0.java内存模型与线程.html">
            
                <a href="../../多线程/basic/0.java内存模型与线程.html">
            
                    
                    2.java内存模型与线程
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="6.3" data-path="../../多线程/3.并行程序基础.html">
            
                <a href="../../多线程/3.并行程序基础.html">
            
                    
                    2.并行程序基础
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="6.4" data-path="../../多线程/basic/0.jhappenBefore与jvm指令重排.html">
            
                <a href="../../多线程/basic/0.jhappenBefore与jvm指令重排.html">
            
                    
                    3.happenBefore与jvm指令重排序
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="6.5" data-path="../../多线程/basic/1.synchronized与volatile.html">
            
                <a href="../../多线程/basic/1.synchronized与volatile.html">
            
                    
                    4.synchronized与volatile.md
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="6.6" data-path="../../多线程/basic/2.ThreadLocal.html">
            
                <a href="../../多线程/basic/2.ThreadLocal.html">
            
                    
                    5.ThreadLocal.md
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="6.7" data-path="../../多线程/cas/1.乐观锁与悲观锁.html">
            
                <a href="../../多线程/cas/1.乐观锁与悲观锁.html">
            
                    
                    6.乐观锁与悲观锁.md
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="6.8" data-path="../../多线程/cas/2.CAS与atomic类.html">
            
                <a href="../../多线程/cas/2.CAS与atomic类.html">
            
                    
                    7.CAS与atomic类.md
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="6.9" data-path="../../多线程/aqs/1.AQS.html">
            
                <a href="../../多线程/aqs/1.AQS.html">
            
                    
                    8.AQS.md
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="6.10" data-path="../../多线程/aqs/2.基于AQS的并发工具.html">
            
                <a href="../../多线程/aqs/2.基于AQS的并发工具.html">
            
                    
                    9.基于AQS的并发工具.md
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="6.11" data-path="../../多线程/blockingQueue/1.阻塞队列.html">
            
                <a href="../../多线程/blockingQueue/1.阻塞队列.html">
            
                    
                    10.阻塞队列.md
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="6.12" data-path="../../多线程/pond/1.线程池与底层原理.html">
            
                <a href="../../多线程/pond/1.线程池与底层原理.html">
            
                    
                    11.线程池与底层原理.md
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="6.13" data-path="../../多线程/10面试题一.html">
            
                <a href="../../多线程/10面试题一.html">
            
                    
                    12.面试题一
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="6.14" data-path="../../多线程/10面试题二.html">
            
                <a href="../../多线程/10面试题二.html">
            
                    
                    13.面试题二.md
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">java集合篇</li>
        
        
    
        <li class="chapter " data-level="7.1" data-path="1.java集合.html">
            
                <a href="1.java集合.html">
            
                    
                    1.集合架构
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="7.2" data-path="2.HashMap源码分析.html">
            
                <a href="2.HashMap源码分析.html">
            
                    
                    2.hashMap底层原理
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="7.3" data-path="3.ConcurrentHashMap分析.html">
            
                <a href="3.ConcurrentHashMap分析.html">
            
                    
                    3.ConcurrentHashMap分析
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">设计模式篇</li>
        
        
    
        <li class="chapter " data-level="8.1" >
            
                <span>
            
                    
                    1.创造型模式
            
                </span>
            

            
        </li>
    
        <li class="chapter " data-level="8.2" data-path="../../设计模式/factory/三种工厂模式介绍.html">
            
                <a href="../../设计模式/factory/三种工厂模式介绍.html">
            
                    
                    三种工厂模式
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="8.3" data-path="../../设计模式/factory/实战工厂模式.html">
            
                <a href="../../设计模式/factory/实战工厂模式.html">
            
                    
                    实战工厂模式
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="8.4" data-path="../../设计模式/factory/实战抽象工厂模式.html">
            
                <a href="../../设计模式/factory/实战抽象工厂模式.html">
            
                    
                    实战抽象工厂模式
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">mysql篇</li>
        
        
    
        <li class="chapter " data-level="9.1" data-path="../../数据库/mysql/1.b树b+树的底层原理.html">
            
                <a href="../../数据库/mysql/1.b树b+树的底层原理.html">
            
                    
                    1.mysql索引基础之b树与b+树⭐⭐
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="9.2" data-path="../../数据库/mysql/2.mysql索引.html">
            
                <a href="../../数据库/mysql/2.mysql索引.html">
            
                    
                    2.mysql索引及优化⭐⭐⭐⭐
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="9.3" data-path="../../数据库/mysql/3.mysql存储引擎.html">
            
                <a href="../../数据库/mysql/3.mysql存储引擎.html">
            
                    
                    3.mysql存储引擎.md
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="9.4" data-path="../../数据库/mysql/4.多版本并发控制之mvcc原理.html">
            
                <a href="../../数据库/mysql/4.多版本并发控制之mvcc原理.html">
            
                    
                    4.多版本并发控制之mvcc原理
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="9.5" data-path="../../数据库/mysql/4.mysql事务.html">
            
                <a href="../../数据库/mysql/4.mysql事务.html">
            
                    
                    5.mysql事务
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="9.6" data-path="../../数据库/mysql/5.mysql锁机制.html">
            
                <a href="../../数据库/mysql/5.mysql锁机制.html">
            
                    
                    6.mysql的锁机制
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="9.7" data-path="../../数据库/mysql/6.mysql面试题一.html">
            
                <a href="../../数据库/mysql/6.mysql面试题一.html">
            
                    
                    7.mysql面试题一
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">高并发中间件篇</li>
        
        
    

    
        
        <li class="header">架构理论篇</li>
        
        
    
        <li class="chapter " data-level="11.1" data-path="../../架构/0.架构演进.html">
            
                <a href="../../架构/0.架构演进.html">
            
                    
                    1.架构演进
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="11.2" data-path="../../架构/0.微服务架构与soa架构.html">
            
                <a href="../../架构/0.微服务架构与soa架构.html">
            
                    
                    2.微服务架构与soa架构
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">高可用系统篇</li>
        
        
    
        <li class="chapter " data-level="12.1" data-path="../../高可用架构/1.限流.html">
            
                <a href="../../高可用架构/1.限流.html">
            
                    
                    1.系统限流
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="12.2" data-path="../../高可用系统/2.服务熔断.html">
            
                <a href="../../高可用系统/2.服务熔断.html">
            
                    
                    2.服务熔断
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="12.3" data-path="../../高可用系统/3.服务降级.html">
            
                <a href="../../高可用系统/3.服务降级.html">
            
                    
                    3.服务降级
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">spring-cloud 微服务篇</li>
        
        
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            本书使用 GitBook 发布
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="../.." >3.ConcurrentHashMap分析</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <div id="anchor-navigation-ex-navbar"><i class="fa fa-navicon"></i><ul><ul><li><span class="title-icon "></span><a href="#&#x524D;&#x8A00;"><b></b>&#x524D;&#x8A00;</a></li><li><span class="title-icon "></span><a href="#concurrenthashmap"><b></b>ConcurrentHashMap</a></li><ul><li><span class="title-icon "></span><a href="#jdk17&#x7684;&#x5B9E;&#x73B0;"><b></b>JDK1.7&#x7684;&#x5B9E;&#x73B0;</a></li><li><span class="title-icon "></span><a href="#jdk18&#x7684;&#x5B9E;&#x73B0;"><b></b>JDK1.8&#x7684;&#x5B9E;&#x73B0;</a></li></ul><li><span class="title-icon "></span><a href="#&#x603B;&#x7ED3;&#x4E0E;&#x601D;&#x8003;"><b></b>&#x603B;&#x7ED3;&#x4E0E;&#x601D;&#x8003;</a></li></ul></ul></div><a href="#" id="anchorNavigationExGoTop"><i class="fa fa-arrow-up"></i></a><h2 id="&#x524D;&#x8A00;"><a name="&#x524D;&#x8A00;" class="anchor-navigation-ex-anchor" href="#&#x524D;&#x8A00;"><i class="fa fa-link" aria-hidden="true"></i></a><a name="&#x524D;&#x8A00;" class="plugin-anchor" href="#&#x524D;&#x8A00;"><i class="fa fa-link" aria-hidden="true"></i></a>&#x524D;&#x8A00;</h2>
<p>&#x6211;&#x4EEC;&#x90FD;&#x77E5;&#x9053;HashMap&#x5728;&#x591A;&#x7EBF;&#x7A0B;&#x60C5;&#x51B5;&#x4E0B;&#xFF0C;&#x5728;put&#x7684;&#x65F6;&#x5019;&#xFF0C;&#x63D2;&#x5165;&#x7684;&#x5143;&#x7D20;&#x8D85;&#x8FC7;&#x4E86;&#x5BB9;&#x91CF;&#xFF08;&#x7531;&#x8D1F;&#x8F7D;&#x56E0;&#x5B50;&#x51B3;&#x5B9A;&#xFF09;&#x7684;&#x8303;&#x56F4;&#x5C31;&#x4F1A;&#x89E6;&#x53D1;&#x6269;&#x5BB9;&#x64CD;&#x4F5C;&#xFF0C;&#x5C31;&#x662F;rehash&#xFF0C;&#x8FD9;&#x4E2A;&#x4F1A;&#x91CD;&#x65B0;&#x5C06;&#x539F;&#x6570;&#x7EC4;&#x7684;&#x5185;&#x5BB9;&#x91CD;&#x65B0;hash&#x5230;&#x65B0;&#x7684;&#x6269;&#x5BB9;&#x6570;&#x7EC4;&#x4E2D;&#xFF0C;&#x5728;&#x591A;&#x7EBF;&#x7A0B;&#x7684;&#x73AF;&#x5883;&#x4E0B;&#xFF0C;&#x5B58;&#x5728;&#x540C;&#x65F6;&#x5176;&#x4ED6;&#x7684;&#x5143;&#x7D20;&#x4E5F;&#x5728;&#x8FDB;&#x884C;put&#x64CD;&#x4F5C;&#xFF0C;&#x5982;&#x679C;hash&#x503C;&#x76F8;&#x540C;&#xFF0C;&#x53EF;&#x80FD;&#x51FA;&#x73B0;&#x540C;&#x65F6;&#x5728;&#x540C;&#x4E00;&#x6570;&#x7EC4;&#x4E0B;&#x7528;&#x94FE;&#x8868;&#x8868;&#x793A;&#xFF0C;&#x9020;&#x6210;&#x95ED;&#x73AF;&#xFF0C;&#x5BFC;&#x81F4;&#x5728;get&#x65F6;&#x4F1A;&#x51FA;&#x73B0;&#x6B7B;&#x5FAA;&#x73AF;&#xFF0C;&#x6240;&#x4EE5;HashMap&#x662F;&#x7EBF;&#x7A0B;&#x4E0D;&#x5B89;&#x5168;&#x7684;&#x3002;</p>
<p>&#x6211;&#x4EEC;&#x6765;&#x4E86;&#x89E3;&#x53E6;&#x4E00;&#x4E2A;&#x952E;&#x503C;&#x5B58;&#x50A8;&#x96C6;&#x5408;HashTable&#xFF0C;&#x5B83;&#x662F;&#x7EBF;&#x7A0B;&#x5B89;&#x5168;&#x7684;&#xFF0C;&#x5B83;&#x5728;&#x6240;&#x6709;&#x6D89;&#x53CA;&#x5230;&#x591A;&#x7EBF;&#x7A0B;&#x64CD;&#x4F5C;&#x7684;&#x90FD;&#x52A0;&#x4E0A;&#x4E86;synchronized&#x5173;&#x952E;&#x5B57;&#x6765;&#x9501;&#x4F4F;&#x6574;&#x4E2A;table&#xFF0C;&#x8FD9;&#x5C31;&#x610F;&#x5473;&#x7740;&#x6240;&#x6709;&#x7684;&#x7EBF;&#x7A0B;&#x90FD;&#x5728;&#x7ADE;&#x4E89;&#x4E00;&#x628A;&#x9501;&#xFF0C;&#x5728;&#x591A;&#x7EBF;&#x7A0B;&#x7684;&#x73AF;&#x5883;&#x4E0B;&#xFF0C;&#x5B83;&#x662F;&#x5B89;&#x5168;&#x7684;&#xFF0C;&#x4F46;&#x662F;&#x65E0;&#x7591;&#x662F;&#x6548;&#x7387;&#x4F4E;&#x4E0B;&#x7684;&#x3002;</p>
<p>&#x5176;&#x5B9E;HashTable&#x6709;&#x5F88;&#x591A;&#x7684;&#x4F18;&#x5316;&#x7A7A;&#x95F4;&#xFF0C;&#x9501;&#x4F4F;&#x6574;&#x4E2A;table&#x8FD9;&#x4E48;&#x7C97;&#x66B4;&#x7684;&#x65B9;&#x6CD5;&#x53EF;&#x4EE5;&#x53D8;&#x76F8;&#x7684;&#x67D4;&#x548C;&#x70B9;&#xFF0C;&#x6BD4;&#x5982;&#x5728;&#x591A;&#x7EBF;&#x7A0B;&#x7684;&#x73AF;&#x5883;&#x4E0B;&#xFF0C;&#x5BF9;&#x4E0D;&#x540C;&#x7684;&#x6570;&#x636E;&#x96C6;&#x8FDB;&#x884C;&#x64CD;&#x4F5C;&#x65F6;&#x5176;&#x5B9E;&#x6839;&#x672C;&#x5C31;&#x4E0D;&#x9700;&#x8981;&#x53BB;&#x7ADE;&#x4E89;&#x4E00;&#x4E2A;&#x9501;&#xFF0C;&#x56E0;&#x4E3A;&#x4ED6;&#x4EEC;&#x4E0D;&#x540C;hash&#x503C;&#xFF0C;&#x4E0D;&#x4F1A;&#x56E0;&#x4E3A;rehash&#x9020;&#x6210;&#x7EBF;&#x7A0B;&#x4E0D;&#x5B89;&#x5168;&#xFF0C;&#x6240;&#x4EE5;&#x4E92;&#x4E0D;&#x5F71;&#x54CD;&#xFF0C;&#x8FD9;&#x5C31;&#x662F;&#x9501;&#x5206;&#x79BB;&#x6280;&#x672F;&#xFF0C;&#x5C06;&#x9501;&#x7684;&#x7C92;&#x5EA6;&#x964D;&#x4F4E;&#xFF0C;&#x5229;&#x7528;&#x591A;&#x4E2A;&#x9501;&#x6765;&#x63A7;&#x5236;&#x591A;&#x4E2A;&#x5C0F;&#x7684;table</p>
<h2 id="concurrenthashmap"><a name="concurrenthashmap" class="anchor-navigation-ex-anchor" href="#concurrenthashmap"><i class="fa fa-link" aria-hidden="true"></i></a><a name="concurrenthashmap" class="plugin-anchor" href="#concurrenthashmap"><i class="fa fa-link" aria-hidden="true"></i></a>ConcurrentHashMap</h2>
<h3 id="jdk17&#x7684;&#x5B9E;&#x73B0;"><a name="jdk17&#x7684;&#x5B9E;&#x73B0;" class="anchor-navigation-ex-anchor" href="#jdk17&#x7684;&#x5B9E;&#x73B0;"><i class="fa fa-link" aria-hidden="true"></i></a><a name="jdk17&#x7684;&#x5B9E;&#x73B0;" class="plugin-anchor" href="#jdk17&#x7684;&#x5B9E;&#x73B0;"><i class="fa fa-link" aria-hidden="true"></i></a>JDK1.7&#x7684;&#x5B9E;&#x73B0;</h3>
<p>&#x5728;JDK1.7&#x7248;&#x672C;&#x4E2D;&#xFF0C;ConcurrentHashMap&#x7684;&#x6570;&#x636E;&#x7ED3;&#x6784;&#x662F;&#x7531;&#x4E00;&#x4E2A;Segment&#x6570;&#x7EC4;&#x548C;&#x591A;&#x4E2A;HashEntry&#x7EC4;&#x6210;&#xFF0C;&#x5982;&#x4E0B;&#x56FE;&#x6240;&#x793A;&#xFF1A;</p>
<p><img src="https://gitee.com/zisuu/picture/raw/master/img/20201222151104.png" alt="image-20201222151104759"></p>
<p>Segment&#x6570;&#x7EC4;&#x7684;&#x610F;&#x4E49;&#x5C31;&#x662F;&#x5C06;&#x4E00;&#x4E2A;&#x5927;&#x7684;table&#x5206;&#x5272;&#x6210;&#x591A;&#x4E2A;&#x5C0F;&#x7684;table&#x6765;&#x8FDB;&#x884C;&#x52A0;&#x9501;&#xFF0C;&#x4E5F;&#x5C31;&#x662F;&#x4E0A;&#x9762;&#x7684;&#x63D0;&#x5230;&#x7684;&#x9501;&#x5206;&#x79BB;&#x6280;&#x672F;,&#x4E5F;&#x5373;&#x6240;&#x8C13;&#x7684;<strong>&#x5206;&#x6BB5;&#x9501;</strong>&#xFF0C;&#x800C;&#x6BCF;&#x4E00;&#x4E2A;Segment&#x5143;&#x7D20;&#x5B58;&#x50A8;&#x7684;&#x662F;HashEntry&#x6570;&#x7EC4;+&#x94FE;&#x8868;&#xFF0C;&#x8FD9;&#x4E2A;&#x548C;HashMap&#x7684;&#x6570;&#x636E;&#x5B58;&#x50A8;&#x7ED3;&#x6784;&#x4E00;&#x6837;</p>
<h4 id="&#x521D;&#x59CB;&#x5316;"><a name="&#x521D;&#x59CB;&#x5316;" class="anchor-navigation-ex-anchor" href="#&#x521D;&#x59CB;&#x5316;"><i class="fa fa-link" aria-hidden="true"></i></a><a name="&#x521D;&#x59CB;&#x5316;" class="plugin-anchor" href="#&#x521D;&#x59CB;&#x5316;"><i class="fa fa-link" aria-hidden="true"></i></a>&#x521D;&#x59CB;&#x5316;</h4>
<p>ConcurrentHashMap&#x7684;&#x521D;&#x59CB;&#x5316;&#x662F;&#x4F1A;&#x901A;&#x8FC7;&#x4F4D;&#x4E0E;&#x8FD0;&#x7B97;&#x6765;&#x521D;&#x59CB;&#x5316;Segment&#x7684;&#x5927;&#x5C0F;&#xFF0C;&#x7528;size&#x6765;&#x8868;&#x793A;&#xFF0C;&#x5982;&#x4E0B;&#x6240;&#x793A;</p>
<pre><code class="lang-int">int size =1;
while(size &lt; concurrencyLevel) {
++a;
size &lt;&lt;=1;
}
</code></pre>
<p>&#x5982;&#x4E0A;&#x6240;&#x793A;&#xFF0C;&#x56E0;&#x4E3A;size&#x7528;&#x4F4D;&#x4E8E;&#x8FD0;&#x7B97;&#x6765;&#x8BA1;&#x7B97;&#xFF08;  size &lt;&lt;=1  &#xFF09;&#xFF0C;&#x6240;&#x4EE5;Segment&#x7684;&#x5927;&#x5C0F;&#x53D6;&#x503C;&#x90FD;&#x662F;&#x4EE5;2&#x7684;N&#x6B21;&#x65B9;&#xFF0C;&#x65E0;&#x5173;concurrencyLevel&#x7684;&#x53D6;&#x503C;&#xFF0C;&#x5F53;&#x7136;concurrencyLevel&#x6700;&#x5927;&#x53EA;&#x80FD;&#x7528;16&#x4F4D;&#x7684;&#x4E8C;&#x8FDB;&#x5236;&#x6765;&#x8868;&#x793A;&#xFF0C;&#x5373;65536&#xFF0C;&#x6362;&#x53E5;&#x8BDD;&#x8BF4;&#xFF0C;Segment&#x7684;&#x5927;&#x5C0F;&#x6700;&#x591A;65536&#x4E2A;&#xFF0C;&#x6CA1;&#x6709;&#x6307;&#x5B9A;concurrencyLevel&#x5143;&#x7D20;&#x521D;&#x59CB;&#x5316;&#xFF0C;Segment&#x7684;&#x5927;&#x5C0F;size&#x9ED8;&#x8BA4;&#x4E3A;16</p>
<p>&#x6BCF;&#x4E00;&#x4E2A;Segment&#x5143;&#x7D20;&#x4E0B;&#x7684;HashEntry&#x7684;&#x521D;&#x59CB;&#x5316;&#x4E5F;&#x662F;&#x6309;&#x7167;&#x4F4D;&#x4E8E;&#x8FD0;&#x7B97;&#x6765;&#x8BA1;&#x7B97;&#xFF0C;&#x7528;cap&#x6765;&#x8868;&#x793A;&#xFF0C;&#x5982;&#x4E0B;&#x6240;&#x793A;</p>
<pre><code class="lang-bash">int <span class="hljs-built_in">cap</span> =1;
<span class="hljs-keyword">while</span>(<span class="hljs-built_in">cap</span> &lt; c)
<span class="hljs-built_in">cap</span> &lt;&lt;=1;
</code></pre>
<p>&#x5982;&#x4E0A;&#x6240;&#x793A;&#xFF0C;HashEntry&#x5927;&#x5C0F;&#x7684;&#x8BA1;&#x7B97;&#x4E5F;&#x662F;2&#x7684;N&#x6B21;&#x65B9;&#xFF08;cap &lt;&lt;=1&#xFF09;&#xFF0C; cap&#x7684;&#x521D;&#x59CB;&#x503C;&#x4E3A;1&#xFF0C;&#x6240;&#x4EE5;HashEntry&#x6700;&#x5C0F;&#x7684;&#x5BB9;&#x91CF;&#x4E3A;2</p>
<h4 id="put&#x64CD;&#x4F5C;"><a name="put&#x64CD;&#x4F5C;" class="anchor-navigation-ex-anchor" href="#put&#x64CD;&#x4F5C;"><i class="fa fa-link" aria-hidden="true"></i></a><a name="put&#x64CD;&#x4F5C;" class="plugin-anchor" href="#put&#x64CD;&#x4F5C;"><i class="fa fa-link" aria-hidden="true"></i></a>put&#x64CD;&#x4F5C;</h4>
<p>&#x5BF9;&#x4E8E;ConcurrentHashMap&#x7684;&#x6570;&#x636E;&#x63D2;&#x5165;&#xFF0C;&#x8FD9;&#x91CC;&#x8981;&#x8FDB;&#x884C;&#x4E24;&#x6B21;Hash&#x53BB;&#x5B9A;&#x4F4D;&#x6570;&#x636E;&#x7684;&#x5B58;&#x50A8;&#x4F4D;&#x7F6E;</p>
<pre><code class="lang-dart"><span class="hljs-keyword">static</span> <span class="hljs-class"><span class="hljs-keyword">class</span>  <span class="hljs-title">Segment</span>&lt;<span class="hljs-title">K</span>,<span class="hljs-title">V</span>&gt; <span class="hljs-keyword">extends</span>  <span class="hljs-title">ReentrantLock</span> <span class="hljs-keyword">implements</span>  <span class="hljs-title">Serializable</span> </span>{
}
</code></pre>
<p>&#x4ECE;&#x4E0A;Segment&#x7684;&#x7EE7;&#x627F;&#x4F53;&#x7CFB;&#x53EF;&#x4EE5;&#x770B;&#x51FA;&#xFF0C;Segment&#x5B9E;&#x73B0;&#x4E86;ReentrantLock,&#x4E5F;&#x5C31;&#x5E26;&#x6709;&#x9501;&#x7684;&#x529F;&#x80FD;&#xFF0C;&#x5F53;&#x6267;&#x884C;put&#x64CD;&#x4F5C;&#x65F6;&#xFF0C;<strong>&#x4F1A;&#x8FDB;&#x884C;&#x7B2C;&#x4E00;&#x6B21;key&#x7684;hash&#x6765;&#x5B9A;&#x4F4D;Segment&#x7684;&#x4F4D;&#x7F6E;&#xFF0C;</strong>&#x5982;&#x679C;&#x8BE5;Segment&#x8FD8;&#x6CA1;&#x6709;&#x521D;&#x59CB;&#x5316;&#xFF0C;<strong>&#x5373;&#x901A;&#x8FC7;CAS&#x64CD;&#x4F5C;&#x8FDB;&#x884C;&#x8D4B;&#x503C;</strong>&#xFF0C;&#x7136;&#x540E;&#x8FDB;&#x884C;<strong>&#x7B2C;&#x4E8C;&#x6B21;hash&#x64CD;&#x4F5C;&#xFF0C;&#x627E;&#x5230;&#x76F8;&#x5E94;&#x7684;HashEntry&#x7684;&#x4F4D;&#x7F6E;</strong>&#xFF0C;&#x8FD9;&#x91CC;&#x4F1A;&#x5229;&#x7528;&#x7EE7;&#x627F;&#x8FC7;&#x6765;&#x7684;&#x9501;&#x7684;&#x7279;&#x6027;&#xFF0C;&#x5728;&#x5C06;&#x6570;&#x636E;&#x63D2;&#x5165;&#x6307;&#x5B9A;&#x7684;HashEntry&#x4F4D;&#x7F6E;&#x65F6;&#xFF08;&#x94FE;&#x8868;&#x7684;&#x5C3E;&#x7AEF;&#xFF09;&#xFF0C;&#x4F1A;&#x901A;&#x8FC7;&#x7EE7;&#x627F;ReentrantLock&#x7684;tryLock&#xFF08;&#xFF09;&#x65B9;&#x6CD5;&#x5C1D;&#x8BD5;&#x53BB;&#x83B7;&#x53D6;&#x9501;&#xFF0C;&#x5982;&#x679C;&#x83B7;&#x53D6;&#x6210;&#x529F;&#x5C31;&#x76F4;&#x63A5;&#x63D2;&#x5165;&#x76F8;&#x5E94;&#x7684;&#x4F4D;&#x7F6E;&#xFF0C;&#x5982;&#x679C;&#x5DF2;&#x7ECF;&#x6709;&#x7EBF;&#x7A0B;&#x83B7;&#x53D6;&#x8BE5;Segment&#x7684;&#x9501;&#xFF0C;&#x90A3;&#x5F53;&#x524D;&#x7EBF;&#x7A0B;&#x4F1A;&#x4EE5;&#x81EA;&#x65CB;&#x7684;&#x65B9;&#x5F0F;&#x53BB;&#x7EE7;&#x7EED;&#x7684;&#x8C03;&#x7528;tryLock&#xFF08;&#xFF09;&#x65B9;&#x6CD5;&#x53BB;&#x83B7;&#x53D6;&#x9501;&#xFF0C;&#x8D85;&#x8FC7;&#x6307;&#x5B9A;&#x6B21;&#x6570;&#x5C31;&#x6302;&#x8D77;&#xFF0C;&#x7B49;&#x5F85;&#x5524;&#x9192;</p>
<h4 id="get&#x64CD;&#x4F5C;"><a name="get&#x64CD;&#x4F5C;" class="anchor-navigation-ex-anchor" href="#get&#x64CD;&#x4F5C;"><i class="fa fa-link" aria-hidden="true"></i></a><a name="get&#x64CD;&#x4F5C;" class="plugin-anchor" href="#get&#x64CD;&#x4F5C;"><i class="fa fa-link" aria-hidden="true"></i></a>get&#x64CD;&#x4F5C;</h4>
<p>ConcurrentHashMap&#x7684;get&#x64CD;&#x4F5C;&#x8DDF;HashMap&#x7C7B;&#x4F3C;&#xFF0C;&#x53EA;&#x662F;ConcurrentHashMap&#x7B2C;&#x4E00;&#x6B21;&#x9700;&#x8981;&#x7ECF;&#x8FC7;&#x4E00;&#x6B21;hash&#x5B9A;&#x4F4D;&#x5230;Segment&#x7684;&#x4F4D;&#x7F6E;&#xFF0C;&#x7136;&#x540E;&#x518D;hash&#x5B9A;&#x4F4D;&#x5230;&#x6307;&#x5B9A;&#x7684;HashEntry&#xFF0C;&#x904D;&#x5386;&#x8BE5;HashEntry&#x4E0B;&#x7684;&#x94FE;&#x8868;&#x8FDB;&#x884C;&#x5BF9;&#x6BD4;&#xFF0C;&#x6210;&#x529F;&#x5C31;&#x8FD4;&#x56DE;&#xFF0C;&#x4E0D;&#x6210;&#x529F;&#x5C31;&#x8FD4;&#x56DE;null</p>
<h4 id="size&#x64CD;&#x4F5C;"><a name="size&#x64CD;&#x4F5C;" class="anchor-navigation-ex-anchor" href="#size&#x64CD;&#x4F5C;"><i class="fa fa-link" aria-hidden="true"></i></a><a name="size&#x64CD;&#x4F5C;" class="plugin-anchor" href="#size&#x64CD;&#x4F5C;"><i class="fa fa-link" aria-hidden="true"></i></a>size&#x64CD;&#x4F5C;</h4>
<p>&#x8BA1;&#x7B97;ConcurrentHashMap&#x7684;&#x5143;&#x7D20;&#x5927;&#x5C0F;&#x662F;&#x4E00;&#x4E2A;&#x6709;&#x8DA3;&#x7684;&#x95EE;&#x9898;&#xFF0C;&#x56E0;&#x4E3A;&#x4ED6;&#x662F;&#x5E76;&#x53D1;&#x64CD;&#x4F5C;&#x7684;&#xFF0C;&#x5C31;&#x662F;&#x5728;&#x4F60;&#x8BA1;&#x7B97;size&#x7684;&#x65F6;&#x5019;&#xFF0C;&#x4ED6;&#x8FD8;&#x5728;&#x5E76;&#x53D1;&#x7684;&#x63D2;&#x5165;&#x6570;&#x636E;&#xFF0C;&#x53EF;&#x80FD;&#x4F1A;&#x5BFC;&#x81F4;&#x4F60;&#x8BA1;&#x7B97;&#x51FA;&#x6765;&#x7684;size&#x548C;&#x4F60;&#x5B9E;&#x9645;&#x7684;size&#x6709;&#x76F8;&#x5DEE;&#xFF08;&#x5728;&#x4F60;return size&#x7684;&#x65F6;&#x5019;&#xFF0C;&#x63D2;&#x5165;&#x4E86;&#x591A;&#x4E2A;&#x6570;&#x636E;&#xFF09;&#xFF0C;&#x8981;&#x89E3;&#x51B3;&#x8FD9;&#x4E2A;&#x95EE;&#x9898;&#xFF0C;JDK1.7&#x7248;&#x672C;&#x7528;&#x4E24;&#x79CD;&#x65B9;&#x6848;</p>
<ol>
<li>&#x7B2C;&#x4E00;&#x79CD;&#x65B9;&#x6848;&#x4ED6;&#x4F1A;&#x4F7F;&#x7528;&#x4E0D;&#x52A0;&#x9501;&#x7684;&#x6A21;&#x5F0F;&#x53BB;&#x5C1D;&#x8BD5;&#x591A;&#x6B21;&#x8BA1;&#x7B97;ConcurrentHashMap&#x7684;size&#xFF0C;&#x6700;&#x591A;&#x4E09;&#x6B21;&#xFF0C;&#x6BD4;&#x8F83;&#x524D;&#x540E;&#x4E24;&#x6B21;&#x8BA1;&#x7B97;&#x7684;&#x7ED3;&#x679C;&#xFF0C;&#x7ED3;&#x679C;&#x4E00;&#x81F4;&#x5C31;&#x8BA4;&#x4E3A;&#x5F53;&#x524D;&#x6CA1;&#x6709;&#x5143;&#x7D20;&#x52A0;&#x5165;&#xFF0C;&#x8BA1;&#x7B97;&#x7684;&#x7ED3;&#x679C;&#x662F;&#x51C6;&#x786E;&#x7684;</li>
<li>&#x7B2C;&#x4E8C;&#x79CD;&#x65B9;&#x6848;&#x662F;&#x5982;&#x679C;&#x7B2C;&#x4E00;&#x79CD;&#x65B9;&#x6848;&#x4E0D;&#x7B26;&#x5408;&#xFF0C;&#x4ED6;&#x5C31;&#x4F1A;&#x7ED9;&#x6BCF;&#x4E2A;Segment&#x52A0;&#x4E0A;&#x9501;&#xFF0C;&#x7136;&#x540E;&#x8BA1;&#x7B97;ConcurrentHashMap&#x7684;size&#x8FD4;&#x56DE;</li>
</ol>
<h3 id="jdk18&#x7684;&#x5B9E;&#x73B0;"><a name="jdk18&#x7684;&#x5B9E;&#x73B0;" class="anchor-navigation-ex-anchor" href="#jdk18&#x7684;&#x5B9E;&#x73B0;"><i class="fa fa-link" aria-hidden="true"></i></a><a name="jdk18&#x7684;&#x5B9E;&#x73B0;" class="plugin-anchor" href="#jdk18&#x7684;&#x5B9E;&#x73B0;"><i class="fa fa-link" aria-hidden="true"></i></a>JDK1.8&#x7684;&#x5B9E;&#x73B0;</h3>
<p>JDK1.8&#x7684;&#x5B9E;&#x73B0;&#x5DF2;&#x7ECF;&#x6452;&#x5F03;&#x4E86;Segment&#x7684;&#x6982;&#x5FF5;&#xFF0C;&#x800C;&#x662F;&#x76F4;&#x63A5;&#x7528;Node&#x6570;&#x7EC4;+&#x94FE;&#x8868;+&#x7EA2;&#x9ED1;&#x6811;&#x7684;&#x6570;&#x636E;&#x7ED3;&#x6784;&#x6765;&#x5B9E;&#x73B0;&#xFF0C;&#x5E76;&#x53D1;&#x63A7;&#x5236;&#x4F7F;&#x7528;Synchronized&#x548C;CAS&#x6765;&#x64CD;&#x4F5C;&#xFF0C;&#x6574;&#x4E2A;&#x770B;&#x8D77;&#x6765;&#x5C31;&#x50CF;&#x662F;&#x4F18;&#x5316;&#x8FC7;&#x4E14;&#x7EBF;&#x7A0B;&#x5B89;&#x5168;&#x7684;HashMap&#xFF0C;&#x867D;&#x7136;&#x5728;JDK1.8&#x4E2D;&#x8FD8;&#x80FD;&#x770B;&#x5230;Segment&#x7684;&#x6570;&#x636E;&#x7ED3;&#x6784;&#xFF0C;&#x4F46;&#x662F;&#x5DF2;&#x7ECF;&#x7B80;&#x5316;&#x4E86;&#x5C5E;&#x6027;&#xFF0C;&#x53EA;&#x662F;&#x4E3A;&#x4E86;&#x517C;&#x5BB9;&#x65E7;&#x7248;&#x672C;</p>
<p><img src="https://gitee.com/zisuu/picture/raw/master/img/20201222151124.png" alt="image-20201222151124524"></p>
<p>&#x8BF4;&#x660E;&#xFF1A;ConcurrentHashMap&#x7684;&#x6570;&#x636E;&#x7ED3;&#x6784;&#xFF08;&#x6570;&#x7EC4;+&#x94FE;&#x8868;+&#x7EA2;&#x9ED1;&#x6811;&#xFF09;&#xFF0C;&#x6876;&#x4E2D;&#x7684;&#x7ED3;&#x6784;&#x53EF;&#x80FD;&#x662F;&#x94FE;&#x8868;&#xFF0C;&#x4E5F;&#x53EF;&#x80FD;&#x662F;&#x7EA2;&#x9ED1;&#x6811;&#xFF0C;&#x7EA2;&#x9ED1;&#x6811;&#x662F;&#x4E3A;&#x4E86;&#x63D0;&#x9AD8;&#x67E5;&#x627E;&#x6548;&#x7387;&#x3002;</p>
<p>&#x5728;&#x6DF1;&#x5165;JDK1.8&#x7684;put&#x548C;get&#x5B9E;&#x73B0;&#x4E4B;&#x524D;&#x8981;&#x77E5;&#x9053;&#x4E00;&#x4E9B;&#x5E38;&#x91CF;&#x8BBE;&#x8BA1;&#x548C;&#x6570;&#x636E;&#x7ED3;&#x6784;&#xFF0C;&#x8FD9;&#x4E9B;&#x662F;&#x6784;&#x6210;ConcurrentHashMap&#x5B9E;&#x73B0;&#x7ED3;&#x6784;&#x7684;&#x57FA;&#x7840;&#xFF0C;&#x4E0B;&#x9762;&#x770B;&#x4E00;&#x4E0B;&#x57FA;&#x672C;&#x5C5E;&#x6027;&#xFF1A;</p>
<pre><code class="lang-java">  <span class="hljs-comment">// node&#x6570;&#x7EC4;&#x6700;&#x5927;&#x5BB9;&#x91CF;&#xFF1A;2^30=1073741824  </span>

  <span class="hljs-keyword">private</span>  <span class="hljs-keyword">static</span>  <span class="hljs-keyword">final</span>  <span class="hljs-keyword">int</span>  MAXIMUM_CAPACITY =  <span class="hljs-number">1</span>  &lt;&lt;  <span class="hljs-number">30</span>    ;  

  <span class="hljs-comment">// &#x9ED8;&#x8BA4;&#x521D;&#x59CB;&#x503C;&#xFF0C;&#x5FC5;&#x987B;&#x662F;2&#x7684;&#x5E42;&#x6570;  </span>

  <span class="hljs-keyword">private</span>  <span class="hljs-keyword">static</span>  <span class="hljs-keyword">final</span>  <span class="hljs-keyword">int</span>  DEFAULT_CAPACITY =  <span class="hljs-number">16</span>    ;  

  <span class="hljs-comment">//&#x6570;&#x7EC4;&#x53EF;&#x80FD;&#x6700;&#x5927;&#x503C;&#xFF0C;&#x9700;&#x8981;&#x4E0E;toArray&#xFF08;&#xFF09;&#x76F8;&#x5173;&#x65B9;&#x6CD5;&#x5173;&#x8054;  </span>

  <span class="hljs-keyword">static</span>  <span class="hljs-keyword">final</span>  <span class="hljs-keyword">int</span>  MAX_ARRAY_SIZE = Integer.MAX_VALUE -  <span class="hljs-number">8</span>    ;  

  <span class="hljs-comment">//&#x5E76;&#x53D1;&#x7EA7;&#x522B;&#xFF0C;&#x9057;&#x7559;&#x4E0B;&#x6765;&#x7684;&#xFF0C;&#x4E3A;&#x517C;&#x5BB9;&#x4EE5;&#x524D;&#x7684;&#x7248;&#x672C;  </span>

  <span class="hljs-keyword">private</span>  <span class="hljs-keyword">static</span>  <span class="hljs-keyword">final</span>  <span class="hljs-keyword">int</span>  DEFAULT_CONCURRENCY_LEVEL =  <span class="hljs-number">16</span>    ;  

  <span class="hljs-comment">// &#x8D1F;&#x8F7D;&#x56E0;&#x5B50;  </span>

  <span class="hljs-keyword">private</span>  <span class="hljs-keyword">static</span>  <span class="hljs-keyword">final</span>  <span class="hljs-keyword">float</span>  LOAD_FACTOR =  <span class="hljs-number">0</span>    .<span class="hljs-number">75f</span>;  

  <span class="hljs-comment">// &#x94FE;&#x8868;&#x8F6C;&#x7EA2;&#x9ED1;&#x6811;&#x9600;&#x503C;,&gt; 8 &#x94FE;&#x8868;&#x8F6C;&#x6362;&#x4E3A;&#x7EA2;&#x9ED1;&#x6811;  </span>

  <span class="hljs-keyword">static</span>  <span class="hljs-keyword">final</span>  <span class="hljs-keyword">int</span>  TREEIFY_THRESHOLD =  <span class="hljs-number">8</span>    ;  

  <span class="hljs-comment">//&#x6811;&#x8F6C;&#x94FE;&#x8868;&#x9600;&#x503C;&#xFF0C;&#x5C0F;&#x4E8E;&#x7B49;&#x4E8E;6&#xFF08;tranfer&#x65F6;&#xFF0C;lc&#x3001;hc=0&#x4E24;&#x4E2A;&#x8BA1;&#x6570;&#x5668;&#x5206;&#x522B;++&#x8BB0;&#x5F55;&#x539F;bin&#x3001;&#x65B0;binTreeNode&#x6570;&#x91CF;&#xFF0C;&lt;=UNTREEIFY_THRESHOLD &#x5219;untreeify(lo)&#xFF09;  </span>

  <span class="hljs-keyword">static</span>  <span class="hljs-keyword">final</span>  <span class="hljs-keyword">int</span>  UNTREEIFY_THRESHOLD =  <span class="hljs-number">6</span>    ;  

  <span class="hljs-keyword">static</span>  <span class="hljs-keyword">final</span>  <span class="hljs-keyword">int</span>  MIN_TREEIFY_CAPACITY =  <span class="hljs-number">64</span>    ;  

  <span class="hljs-keyword">private</span>  <span class="hljs-keyword">static</span>  <span class="hljs-keyword">final</span>  <span class="hljs-keyword">int</span>  MIN_TRANSFER_STRIDE =  <span class="hljs-number">16</span>    ;  

  <span class="hljs-keyword">private</span>  <span class="hljs-keyword">static</span>  <span class="hljs-keyword">int</span>  RESIZE_STAMP_BITS =  <span class="hljs-number">16</span>    ;  

  <span class="hljs-comment">// 2^15-1&#xFF0C;help resize&#x7684;&#x6700;&#x5927;&#x7EBF;&#x7A0B;&#x6570;  </span>

  <span class="hljs-keyword">private</span>  <span class="hljs-keyword">static</span>  <span class="hljs-keyword">final</span>  <span class="hljs-keyword">int</span>  MAX_RESIZERS = (    <span class="hljs-number">1</span>  &lt;&lt; (    <span class="hljs-number">32</span>  - RESIZE_STAMP_BITS)) -  <span class="hljs-number">1</span>    ;  

  <span class="hljs-comment">// 32-16=16&#xFF0C;sizeCtl&#x4E2D;&#x8BB0;&#x5F55;size&#x5927;&#x5C0F;&#x7684;&#x504F;&#x79FB;&#x91CF;  </span>

  <span class="hljs-keyword">private</span>  <span class="hljs-keyword">static</span>  <span class="hljs-keyword">final</span>  <span class="hljs-keyword">int</span>  RESIZE_STAMP_SHIFT =  <span class="hljs-number">32</span>  - RESIZE_STAMP_BITS;  

  <span class="hljs-comment">// forwarding nodes&#x7684;hash&#x503C;  </span>

  <span class="hljs-keyword">static</span>  <span class="hljs-keyword">final</span>  <span class="hljs-keyword">int</span>  MOVED     = -    <span class="hljs-number">1</span>    ;  

  <span class="hljs-comment">// &#x6811;&#x6839;&#x8282;&#x70B9;&#x7684;hash&#x503C;  </span>

  <span class="hljs-keyword">static</span>  <span class="hljs-keyword">final</span>  <span class="hljs-keyword">int</span>  TREEBIN   = -    <span class="hljs-number">2</span>    ;  

  <span class="hljs-comment">// ReservationNode&#x7684;hash&#x503C;  </span>

  <span class="hljs-keyword">static</span>  <span class="hljs-keyword">final</span>  <span class="hljs-keyword">int</span>  RESERVED  = -    <span class="hljs-number">3</span>    ;  

  <span class="hljs-comment">// &#x53EF;&#x7528;&#x5904;&#x7406;&#x5668;&#x6570;&#x91CF;  </span>

  <span class="hljs-keyword">static</span>  <span class="hljs-keyword">final</span>  <span class="hljs-keyword">int</span>  NCPU = Runtime.getRuntime().availableProcessors();  

  <span class="hljs-comment">//&#x5B58;&#x653E;node&#x7684;&#x6570;&#x7EC4;  </span>

  <span class="hljs-keyword">transient</span>  <span class="hljs-keyword">volatile</span>  Node&lt;K,V&gt;[] table;  

  <span class="hljs-comment">/*&#x63A7;&#x5236;&#x6807;&#x8BC6;&#x7B26;&#xFF0C;&#x7528;&#x6765;&#x63A7;&#x5236;table&#x7684;&#x521D;&#x59CB;&#x5316;&#x548C;&#x6269;&#x5BB9;&#x7684;&#x64CD;&#x4F5C;&#xFF0C;&#x4E0D;&#x540C;&#x7684;&#x503C;&#x6709;&#x4E0D;&#x540C;&#x7684;&#x542B;&#x4E49;  

  *&#x5F53;&#x4E3A;&#x8D1F;&#x6570;&#x65F6;&#xFF1A;-    1    &#x4EE3;&#x8868;&#x6B63;&#x5728;&#x521D;&#x59CB;&#x5316;&#xFF0C;-N&#x4EE3;&#x8868;&#x6709;N-    1    &#x4E2A;&#x7EBF;&#x7A0B;&#x6B63;&#x5728; &#x8FDB;&#x884C;&#x6269;&#x5BB9;  

  *&#x5F53;&#x4E3A;    0    &#x65F6;&#xFF1A;&#x4EE3;&#x8868;&#x5F53;&#x65F6;&#x7684;table&#x8FD8;&#x6CA1;&#x6709;&#x88AB;&#x521D;&#x59CB;&#x5316;  

  *&#x5F53;&#x4E3A;&#x6B63;&#x6570;&#x65F6;&#xFF1A;&#x8868;&#x793A;&#x521D;&#x59CB;&#x5316;&#x6216;&#x8005;&#x4E0B;&#x4E00;&#x6B21;&#x8FDB;&#x884C;&#x6269;&#x5BB9;&#x7684;&#x5927;&#x5C0F;  
*/</span>

  <span class="hljs-keyword">private</span>  <span class="hljs-keyword">transient</span>  <span class="hljs-keyword">volatile</span>  <span class="hljs-keyword">int</span>  sizeCtl;
</code></pre>
<p>&#x57FA;&#x672C;&#x5C5E;&#x6027;&#x5B9A;&#x4E49;&#x4E86;ConcurrentHashMap&#x7684;&#x4E00;&#x4E9B;&#x8FB9;&#x754C;&#x4EE5;&#x53CA;&#x64CD;&#x4F5C;&#x65F6;&#x7684;&#x4E00;&#x4E9B;&#x63A7;&#x5236;&#xFF0C;&#x4E0B;&#x9762;&#x770B;&#x4E00;&#x4E9B;&#x5185;&#x90E8;&#x7684;&#x4E00;&#x4E9B;&#x7ED3;&#x6784;&#x7EC4;&#x6210;&#xFF0C;&#x8FD9;&#x4E9B;&#x662F;&#x6574;&#x4E2A;ConcurrentHashMap&#x6574;&#x4E2A;&#x6570;&#x636E;&#x7ED3;&#x6784;&#x7684;&#x6838;&#x5FC3;</p>
<h4 id="node"><a name="node" class="anchor-navigation-ex-anchor" href="#node"><i class="fa fa-link" aria-hidden="true"></i></a><a name="node" class="plugin-anchor" href="#node"><i class="fa fa-link" aria-hidden="true"></i></a>Node</h4>
<p>Node&#x662F;ConcurrentHashMap&#x5B58;&#x50A8;&#x7ED3;&#x6784;&#x7684;&#x57FA;&#x672C;&#x5355;&#x5143;&#xFF0C;&#x7EE7;&#x627F;&#x4E8E;HashMap&#x4E2D;&#x7684;Entry&#xFF0C;&#x7528;&#x4E8E;&#x5B58;&#x50A8;&#x6570;&#x636E;&#xFF0C;&#x6E90;&#x4EE3;&#x7801;&#x5982;&#x4E0B;</p>
<pre><code class="lang-kotlin">  static  class  Node&lt;K,V&gt;  implements  Map.Entry&lt;K,V&gt; {  

  //&#x94FE;&#x8868;&#x7684;&#x6570;&#x636E;&#x7ED3;&#x6784;  

  final  int  hash;  

  final  K key;  

  //val&#x548C;next&#x90FD;&#x4F1A;&#x5728;&#x6269;&#x5BB9;&#x65F6;&#x53D1;&#x751F;&#x53D8;&#x5316;&#xFF0C;&#x6240;&#x4EE5;&#x52A0;&#x4E0A;volatile&#x6765;&#x4FDD;&#x6301;&#x53EF;&#x89C1;&#x6027;&#x548C;&#x7981;&#x6B62;&#x91CD;&#x6392;&#x5E8F;  

  volatile  V val;  

  volatile  Node&lt;K,V&gt; next;  

  Node(    int  hash, K key, V val, Node&lt;K,V&gt; next) {  

  this    .hash = hash;  

  this    .key = key;  

  this    .val = val;  

  this    .next = next;  

  }  

  public  final  K getKey()       {  return  key; }  

  public  final  V getValue()     {  return  val; }  

  public  final  int  hashCode()   {  return  key.hashCode() ^ val.hashCode(); }  

  public  final  String toString(){  return  key +  &quot;=&quot;  + val; }  

  //&#x4E0D;&#x5141;&#x8BB8;&#x66F4;&#x65B0;value   

  public  final  V setValue(V value) {  

  throw  new  UnsupportedOperationException();  

  }  

  public  final  boolean  equals(Object o) {  

  Object k, v, u; Map.Entry&lt;?,?&gt; e;  

  return  ((o  instanceof  Map.Entry) &amp;&amp;  

  (k = (e = (Map.Entry&lt;?,?&gt;)o).getKey()) !=  null  &amp;&amp;  

  (v = e.getValue()) !=  null  &amp;&amp;  

  (k == key || k.equals(key)) &amp;&amp;  

  (v == (u = val) || v.equals(u)));  

  }  

  //&#x7528;&#x4E8E;map&#x4E2D;&#x7684;get&#xFF08;&#xFF09;&#x65B9;&#x6CD5;&#xFF0C;&#x5B50;&#x7C7B;&#x91CD;&#x5199;  

  Node&lt;K,V&gt; find(    int  h, Object k) {  

  Node&lt;K,V&gt; e =  this    ;  

  if  (k !=  null    ) {  

  do  {  

  K ek;  

  if  (e.hash == h &amp;&amp;  

  ((ek = e.key) == k || (ek !=  null  &amp;&amp; k.equals(ek))))  

  return  e;  

  }  while  ((e = e.next) !=  null    );  

  }  

  return  null    ;  

  }  

  }
</code></pre>
<p>Node&#x6570;&#x636E;&#x7ED3;&#x6784;&#x5F88;&#x7B80;&#x5355;&#xFF0C;&#x4ECE;&#x4E0A;&#x53EF;&#x77E5;&#xFF0C;&#x5C31;&#x662F;&#x4E00;&#x4E2A;&#x94FE;&#x8868;&#xFF0C;&#x4F46;&#x662F;&#x53EA;&#x5141;&#x8BB8;&#x5BF9;&#x6570;&#x636E;&#x8FDB;&#x884C;&#x67E5;&#x627E;&#xFF0C;&#x4E0D;&#x5141;&#x8BB8;&#x8FDB;&#x884C;&#x4FEE;&#x6539;</p>
<h4 id="treenode"><a name="treenode" class="anchor-navigation-ex-anchor" href="#treenode"><i class="fa fa-link" aria-hidden="true"></i></a><a name="treenode" class="plugin-anchor" href="#treenode"><i class="fa fa-link" aria-hidden="true"></i></a>TreeNode</h4>
<p>TreeNode&#x7EE7;&#x627F;&#x4E0E;Node&#xFF0C;&#x4F46;&#x662F;&#x6570;&#x636E;&#x7ED3;&#x6784;&#x6362;&#x6210;&#x4E86;&#x4E8C;&#x53C9;&#x6811;&#x7ED3;&#x6784;&#xFF0C;&#x5B83;&#x662F;&#x7EA2;&#x9ED1;&#x6811;&#x7684;&#x6570;&#x636E;&#x7684;&#x5B58;&#x50A8;&#x7ED3;&#x6784;&#xFF0C;&#x7528;&#x4E8E;&#x7EA2;&#x9ED1;&#x6811;&#x4E2D;&#x5B58;&#x50A8;&#x6570;&#x636E;&#xFF0C;&#x5F53;&#x94FE;&#x8868;&#x7684;&#x8282;&#x70B9;&#x6570;&#x5927;&#x4E8E;8&#x65F6;&#x4F1A;&#x8F6C;&#x6362;&#x6210;&#x7EA2;&#x9ED1;&#x6811;&#x7684;&#x7ED3;&#x6784;&#xFF0C;&#x4ED6;&#x5C31;&#x662F;&#x901A;&#x8FC7;TreeNode&#x4F5C;&#x4E3A;&#x5B58;&#x50A8;&#x7ED3;&#x6784;&#x4EE3;&#x66FF;Node&#x6765;&#x8F6C;&#x6362;&#x6210;&#x9ED1;&#x7EA2;&#x6811;&#x6E90;&#x4EE3;&#x7801;&#x5982;&#x4E0B;</p>
<pre><code class="lang-java">  <span class="hljs-keyword">static</span>  <span class="hljs-keyword">final</span>  <span class="hljs-class"><span class="hljs-keyword">class</span>  <span class="hljs-title">TreeNode</span>&lt;<span class="hljs-title">K</span>,<span class="hljs-title">V</span>&gt;  <span class="hljs-keyword">extends</span>  <span class="hljs-title">Node</span>&lt;<span class="hljs-title">K</span>,<span class="hljs-title">V</span>&gt; </span>{  

  <span class="hljs-comment">//&#x6811;&#x5F62;&#x7ED3;&#x6784;&#x7684;&#x5C5E;&#x6027;&#x5B9A;&#x4E49;  </span>

  TreeNode&lt;K,V&gt; parent;  <span class="hljs-comment">// red-black tree links  </span>

  TreeNode&lt;K,V&gt; left;  

  TreeNode&lt;K,V&gt; right;  

  TreeNode&lt;K,V&gt; prev;  <span class="hljs-comment">// needed to unlink next upon deletion  </span>

  <span class="hljs-keyword">boolean</span>  red;  <span class="hljs-comment">//&#x6807;&#x5FD7;&#x7EA2;&#x9ED1;&#x6811;&#x7684;&#x7EA2;&#x8282;&#x70B9;  </span>

  TreeNode(    <span class="hljs-keyword">int</span>  hash, K key, V val, Node&lt;K,V&gt; next,  

  TreeNode&lt;K,V&gt; parent) {  

  <span class="hljs-keyword">super</span>    (hash, key, val, next);  

  <span class="hljs-keyword">this</span>    .parent = parent;  

  }  

  <span class="hljs-function">Node&lt;K,V&gt; <span class="hljs-title">find</span><span class="hljs-params">(    <span class="hljs-keyword">int</span>  h, Object k)</span> </span>{  

  <span class="hljs-keyword">return</span>  findTreeNode(h, k,  <span class="hljs-keyword">null</span>    );  

  }  

  <span class="hljs-comment">//&#x6839;&#x636E;key&#x67E5;&#x627E; &#x4ECE;&#x6839;&#x8282;&#x70B9;&#x5F00;&#x59CB;&#x627E;&#x51FA;&#x76F8;&#x5E94;&#x7684;TreeNode&#xFF0C;  </span>

  <span class="hljs-function"><span class="hljs-keyword">final</span>  TreeNode&lt;K,V&gt; <span class="hljs-title">findTreeNode</span><span class="hljs-params">(    <span class="hljs-keyword">int</span>  h, Object k, Class&lt;?&gt; kc)</span> </span>{  

  <span class="hljs-keyword">if</span>  (k !=  <span class="hljs-keyword">null</span>    ) {  

  TreeNode&lt;K,V&gt; p =  <span class="hljs-keyword">this</span>    ;  

  do      {  

  <span class="hljs-keyword">int</span>  ph, dir; K pk; TreeNode&lt;K,V&gt; q;  

  TreeNode&lt;K,V&gt; pl = p.left, pr = p.right;  

  <span class="hljs-keyword">if</span>  ((ph = p.hash) &gt; h)  

  p = pl;  

  <span class="hljs-keyword">else</span>  <span class="hljs-keyword">if</span>  (ph &lt; h)  

  p = pr;  

  <span class="hljs-keyword">else</span>  <span class="hljs-keyword">if</span>  ((pk = p.key) == k || (pk !=  <span class="hljs-keyword">null</span>  &amp;&amp; k.equals(pk)))  

  <span class="hljs-keyword">return</span>  p;  

  <span class="hljs-keyword">else</span>  <span class="hljs-keyword">if</span>  (pl ==  <span class="hljs-keyword">null</span>    )  

  p = pr;  

  <span class="hljs-keyword">else</span>  <span class="hljs-keyword">if</span>  (pr ==  <span class="hljs-keyword">null</span>    )  

  p = pl;  

  <span class="hljs-keyword">else</span>  <span class="hljs-keyword">if</span>  ((kc !=  <span class="hljs-keyword">null</span>  ||  

  (kc = comparableClassFor(k)) !=  <span class="hljs-keyword">null</span>    ) &amp;&amp;  

  (dir = compareComparables(kc, k, pk)) !=  <span class="hljs-number">0</span>    )  

  p = (dir &lt;  <span class="hljs-number">0</span>    ) ? pl : pr;  

  <span class="hljs-keyword">else</span>  <span class="hljs-keyword">if</span>  ((q = pr.findTreeNode(h, k, kc)) !=  <span class="hljs-keyword">null</span>    )  

  <span class="hljs-keyword">return</span>  q;  

  <span class="hljs-keyword">else</span>  

  p = pl;  

  }  <span class="hljs-keyword">while</span>  (p !=  <span class="hljs-keyword">null</span>    );  

  }  

  <span class="hljs-keyword">return</span>  <span class="hljs-keyword">null</span>    ;  

  }  

  }
</code></pre>
<h4 id="treebin"><a name="treebin" class="anchor-navigation-ex-anchor" href="#treebin"><i class="fa fa-link" aria-hidden="true"></i></a><a name="treebin" class="plugin-anchor" href="#treebin"><i class="fa fa-link" aria-hidden="true"></i></a>TreeBin</h4>
<p>TreeBin&#x4ECE;&#x5B57;&#x9762;&#x542B;&#x4E49;&#x4E2D;&#x53EF;&#x4EE5;&#x7406;&#x89E3;&#x4E3A;&#x5B58;&#x50A8;&#x6811;&#x5F62;&#x7ED3;&#x6784;&#x7684;&#x5BB9;&#x5668;&#xFF0C;&#x800C;&#x6811;&#x5F62;&#x7ED3;&#x6784;&#x5C31;&#x662F;&#x6307;TreeNode&#xFF0C;&#x6240;&#x4EE5;TreeBin&#x5C31;&#x662F;&#x5C01;&#x88C5;TreeNode&#x7684;&#x5BB9;&#x5668;&#xFF0C;&#x5B83;&#x63D0;&#x4F9B;&#x8F6C;&#x6362;&#x9ED1;&#x7EA2;&#x6811;&#x7684;&#x4E00;&#x4E9B;&#x6761;&#x4EF6;&#x548C;&#x9501;&#x7684;&#x63A7;&#x5236;&#xFF0C;&#x90E8;&#x5206;&#x6E90;&#x7801;&#x7ED3;&#x6784;&#x5982;&#x4E0B;</p>
<pre><code class="lang-java">  <span class="hljs-keyword">static</span>  <span class="hljs-keyword">final</span>  <span class="hljs-class"><span class="hljs-keyword">class</span>  <span class="hljs-title">TreeBin</span>&lt;<span class="hljs-title">K</span>,<span class="hljs-title">V</span>&gt;  <span class="hljs-keyword">extends</span>  <span class="hljs-title">Node</span>&lt;<span class="hljs-title">K</span>,<span class="hljs-title">V</span>&gt; </span>{  

  <span class="hljs-comment">//&#x6307;&#x5411;TreeNode&#x5217;&#x8868;&#x548C;&#x6839;&#x8282;&#x70B9;  </span>

  TreeNode&lt;K,V&gt; root;  

  <span class="hljs-keyword">volatile</span>  TreeNode&lt;K,V&gt; first;  

  <span class="hljs-keyword">volatile</span>  Thread waiter;  

  <span class="hljs-keyword">volatile</span>  <span class="hljs-keyword">int</span>  lockState;  

  <span class="hljs-comment">// &#x8BFB;&#x5199;&#x9501;&#x72B6;&#x6001;  </span>

  <span class="hljs-keyword">static</span>  <span class="hljs-keyword">final</span>  <span class="hljs-keyword">int</span>  WRITER =  <span class="hljs-number">1</span>    ;  <span class="hljs-comment">// &#x83B7;&#x53D6;&#x5199;&#x9501;&#x7684;&#x72B6;&#x6001;  </span>

  <span class="hljs-keyword">static</span>  <span class="hljs-keyword">final</span>  <span class="hljs-keyword">int</span>  WAITER =  <span class="hljs-number">2</span>    ;  <span class="hljs-comment">// &#x7B49;&#x5F85;&#x5199;&#x9501;&#x7684;&#x72B6;&#x6001;  </span>

  <span class="hljs-keyword">static</span>  <span class="hljs-keyword">final</span>  <span class="hljs-keyword">int</span>  READER =  <span class="hljs-number">4</span>    ;  <span class="hljs-comment">// &#x589E;&#x52A0;&#x6570;&#x636E;&#x65F6;&#x8BFB;&#x9501;&#x7684;&#x72B6;&#x6001;  </span>

  <span class="hljs-comment">/**  

  * &#x521D;&#x59CB;&#x5316;&#x7EA2;&#x9ED1;&#x6811;  

  */</span>  

  TreeBin(TreeNode&lt;K,V&gt; b) {  

  <span class="hljs-keyword">super</span>    (TREEBIN,  <span class="hljs-keyword">null</span>    ,  <span class="hljs-keyword">null</span>    ,  <span class="hljs-keyword">null</span>    );  

  <span class="hljs-keyword">this</span>    .first = b;  

  TreeNode&lt;K,V&gt; r =  <span class="hljs-keyword">null</span>    ;  

  <span class="hljs-keyword">for</span>  (TreeNode&lt;K,V&gt; x = b, next; x !=  <span class="hljs-keyword">null</span>    ; x = next) {  

  next = (TreeNode&lt;K,V&gt;)x.next;  

  x.left = x.right =  <span class="hljs-keyword">null</span>    ;  

  <span class="hljs-keyword">if</span>  (r ==  <span class="hljs-keyword">null</span>    ) {  

  x.parent =  <span class="hljs-keyword">null</span>    ;  

  x.red =  <span class="hljs-keyword">false</span>    ;  

  r = x;  

  }  

  <span class="hljs-keyword">else</span>  {  

  K k = x.key;  

  <span class="hljs-keyword">int</span>  h = x.hash;  

  Class&lt;?&gt; kc =  <span class="hljs-keyword">null</span>    ;  

  <span class="hljs-keyword">for</span>  (TreeNode&lt;K,V&gt; p = r;;) {  

  <span class="hljs-keyword">int</span>  dir, ph;  

  K pk = p.key;  

  <span class="hljs-keyword">if</span>  ((ph = p.hash) &gt; h)  

  dir = -    <span class="hljs-number">1</span>    ;  

  <span class="hljs-keyword">else</span>  <span class="hljs-keyword">if</span>  (ph &lt; h)  

  dir =  <span class="hljs-number">1</span>    ;  

  <span class="hljs-keyword">else</span>  <span class="hljs-keyword">if</span>  ((kc ==  <span class="hljs-keyword">null</span>  &amp;&amp;  

  (kc = comparableClassFor(k)) ==  <span class="hljs-keyword">null</span>    ) ||  

  (dir = compareComparables(kc, k, pk)) ==  <span class="hljs-number">0</span>    )  

  dir = tieBreakOrder(k, pk);  

  TreeNode&lt;K,V&gt; xp = p;  

  <span class="hljs-keyword">if</span>  ((p = (dir &lt;=  <span class="hljs-number">0</span>    ) ? p.left : p.right) ==  <span class="hljs-keyword">null</span>    ) {  

  x.parent = xp;  

  <span class="hljs-keyword">if</span>  (dir &lt;=  <span class="hljs-number">0</span>    )  

  xp.left = x;  

  <span class="hljs-keyword">else</span>  

  xp.right = x;  

  r = balanceInsertion(r, x);  

  <span class="hljs-keyword">break</span>    ;  

  }  

  }  

  }  

  }  

  <span class="hljs-keyword">this</span>    .root = r;  

  <span class="hljs-function"><span class="hljs-keyword">assert</span>  <span class="hljs-title">checkInvariants</span><span class="hljs-params">(root)</span></span>;  

  }  

  ......  

  }
</code></pre>
<p>&#x4ECB;&#x7ECD;&#x4E86;ConcurrentHashMap&#x4E3B;&#x8981;&#x7684;&#x5C5E;&#x6027;&#x4E0E;&#x5185;&#x90E8;&#x7684;&#x6570;&#x636E;&#x7ED3;&#x6784;&#xFF0C;&#x73B0;&#x5728;&#x901A;&#x8FC7;&#x4E00;&#x4E2A;&#x7B80;&#x5355;&#x7684;&#x4F8B;&#x5B50;&#x4EE5;debug&#x7684;&#x89C6;&#x89D2;&#x770B;&#x770B;ConcurrentHashMap&#x7684;&#x5177;&#x4F53;&#x64CD;&#x4F5C;&#x7EC6;&#x8282;</p>
<pre><code class="lang-cpp">  <span class="hljs-keyword">public</span>  <span class="hljs-keyword">class</span>  TestConcurrentHashMap{     

  <span class="hljs-function"><span class="hljs-keyword">public</span>  <span class="hljs-keyword">static</span>  <span class="hljs-keyword">void</span>  <span class="hljs-title">main</span><span class="hljs-params">(String[] args)</span></span>{  

  ConcurrentHashMap&lt;String,String&gt; <span class="hljs-built_in">map</span> =  <span class="hljs-keyword">new</span>  ConcurrentHashMap();  <span class="hljs-comment">//&#x521D;&#x59CB;&#x5316;ConcurrentHashMap  </span>

  <span class="hljs-comment">//&#x65B0;&#x589E;&#x4E2A;&#x4EBA;&#x4FE1;&#x606F;  </span>

  <span class="hljs-built_in">map</span>.put(    <span class="hljs-string">&quot;id&quot;</span>    ,    <span class="hljs-string">&quot;1&quot;</span>    );  

  <span class="hljs-built_in">map</span>.put(    <span class="hljs-string">&quot;name&quot;</span>    ,    <span class="hljs-string">&quot;andy&quot;</span>    );  

  <span class="hljs-built_in">map</span>.put(    <span class="hljs-string">&quot;sex&quot;</span>    ,    <span class="hljs-string">&quot;&#x7537;&quot;</span>    );  

  <span class="hljs-comment">//&#x83B7;&#x53D6;&#x59D3;&#x540D;  </span>

  String name = <span class="hljs-built_in">map</span>.get(    <span class="hljs-string">&quot;name&quot;</span>    );  

  Assert.assertEquals(name,    <span class="hljs-string">&quot;andy&quot;</span>    );  

  <span class="hljs-comment">//&#x8BA1;&#x7B97;&#x5927;&#x5C0F;  </span>

  <span class="hljs-keyword">int</span>  size = <span class="hljs-built_in">map</span>.size();  

  Assert.assertEquals(size,    <span class="hljs-number">3</span>    );  

  }  

  }
</code></pre>
<p>&#x6211;&#x4EEC;&#x5148;&#x901A;&#x8FC7;  new ConcurrentHashMap()  &#x6765;&#x8FDB;&#x884C;&#x521D;&#x59CB;&#x5316;</p>
<pre><code class="lang-cpp">  <span class="hljs-function"><span class="hljs-keyword">public</span>  <span class="hljs-title">ConcurrentHashMap</span><span class="hljs-params">()</span> </span>{  

  }
</code></pre>
<p>&#x7531;&#x4E0A;&#x4F60;&#x4F1A;&#x53D1;&#x73B0;ConcurrentHashMap&#x7684;&#x521D;&#x59CB;&#x5316;&#x5176;&#x5B9E;&#x662F;&#x4E00;&#x4E2A;&#x7A7A;&#x5B9E;&#x73B0;&#xFF0C;&#x5E76;&#x6CA1;&#x6709;&#x505A;&#x4EFB;&#x4F55;&#x4E8B;&#xFF0C;&#x8FD9;&#x91CC;&#x540E;&#x9762;&#x4F1A;&#x8BB2;&#x5230;&#xFF0C;&#x8FD9;&#x4E5F;&#x662F;&#x548C;&#x5176;&#x4ED6;&#x7684;&#x96C6;&#x5408;&#x7C7B;&#x6709;&#x533A;&#x522B;&#x7684;&#x5730;&#x65B9;&#xFF0C;&#x521D;&#x59CB;&#x5316;&#x64CD;&#x4F5C;&#x5E76;&#x4E0D;&#x662F;&#x5728;&#x6784;&#x9020;&#x51FD;&#x6570;&#x5B9E;&#x73B0;&#x7684;&#xFF0C;&#x800C;&#x662F;&#x5728;put&#x64CD;&#x4F5C;&#x4E2D;&#x5B9E;&#x73B0;&#xFF0C;&#x5F53;&#x7136;ConcurrentHashMap&#x8FD8;&#x63D0;&#x4F9B;&#x4E86;&#x5176;&#x4ED6;&#x7684;&#x6784;&#x9020;&#x51FD;&#x6570;&#xFF0C;&#x6709;&#x6307;&#x5B9A;&#x5BB9;&#x91CF;&#x5927;&#x5C0F;&#x6216;&#x8005;&#x6307;&#x5B9A;&#x8D1F;&#x8F7D;&#x56E0;&#x5B50;&#xFF0C;&#x8DDF;HashMap&#x4E00;&#x6837;&#xFF0C;&#x8FD9;&#x91CC;&#x5C31;&#x4E0D;&#x505A;&#x4ECB;&#x7ECD;&#x4E86;</p>
<h4 id="put&#x64CD;&#x4F5C;_1"><a name="put&#x64CD;&#x4F5C;_1" class="anchor-navigation-ex-anchor" href="#put&#x64CD;&#x4F5C;_1"><i class="fa fa-link" aria-hidden="true"></i></a><a name="put&#x64CD;&#x4F5C;" class="plugin-anchor" href="#put&#x64CD;&#x4F5C;"><i class="fa fa-link" aria-hidden="true"></i></a>put&#x64CD;&#x4F5C;</h4>
<p>&#x5728;&#x4E0A;&#x9762;&#x7684;&#x4F8B;&#x5B50;&#x4E2D;&#x6211;&#x4EEC;&#x65B0;&#x589E;&#x4E2A;&#x4EBA;&#x4FE1;&#x606F;&#x4F1A;&#x8C03;&#x7528;put&#x65B9;&#x6CD5;&#xFF0C;&#x6211;&#x4EEC;&#x6765;&#x770B;&#x4E0B;</p>
<pre><code class="lang-csharp">  <span class="hljs-function"><span class="hljs-keyword">public</span>  V <span class="hljs-title">put</span>(<span class="hljs-params">K key, V <span class="hljs-keyword">value</span></span>) </span>{  

  <span class="hljs-keyword">return</span>  putVal(key, <span class="hljs-keyword">value</span>,  <span class="hljs-keyword">false</span>    );  

  }  

  <span class="hljs-comment">/** Implementation for put and putIfAbsent */</span>  

  <span class="hljs-function">final  V <span class="hljs-title">putVal</span>(<span class="hljs-params">K key, V <span class="hljs-keyword">value</span>,  boolean  onlyIfAbsent</span>) </span>{  

  <span class="hljs-keyword">if</span>  (key ==  <span class="hljs-keyword">null</span>  || <span class="hljs-keyword">value</span> ==  <span class="hljs-keyword">null</span>    )  <span class="hljs-keyword">throw</span>  <span class="hljs-keyword">new</span>  NullPointerException();  

  <span class="hljs-keyword">int</span>  hash = spread(key.hashCode());  <span class="hljs-comment">//&#x4E24;&#x6B21;hash&#xFF0C;&#x51CF;&#x5C11;hash&#x51B2;&#x7A81;&#xFF0C;&#x53EF;&#x4EE5;&#x5747;&#x5300;&#x5206;&#x5E03;  </span>

  <span class="hljs-keyword">int</span>  binCount =  <span class="hljs-number">0</span>    ;  

  <span class="hljs-keyword">for</span>  (Node&lt;K,V&gt;[] tab = table;;) {  <span class="hljs-comment">//&#x5BF9;&#x8FD9;&#x4E2A;table&#x8FDB;&#x884C;&#x8FED;&#x4EE3;  </span>

  Node&lt;K,V&gt; f;  <span class="hljs-keyword">int</span>  n, i, fh;  

  <span class="hljs-comment">//&#x8FD9;&#x91CC;&#x5C31;&#x662F;&#x4E0A;&#x9762;&#x6784;&#x9020;&#x65B9;&#x6CD5;&#x6CA1;&#x6709;&#x8FDB;&#x884C;&#x521D;&#x59CB;&#x5316;&#xFF0C;&#x5728;&#x8FD9;&#x91CC;&#x8FDB;&#x884C;&#x5224;&#x65AD;&#xFF0C;&#x4E3A;null&#x5C31;&#x8C03;&#x7528;initTable&#x8FDB;&#x884C;&#x521D;&#x59CB;&#x5316;&#xFF0C;&#x5C5E;&#x4E8E;&#x61D2;&#x6C49;&#x6A21;&#x5F0F;&#x521D;&#x59CB;&#x5316;  </span>

  <span class="hljs-keyword">if</span>  (tab ==  <span class="hljs-keyword">null</span>  || (n = tab.length) ==  <span class="hljs-number">0</span>    )  

  tab = initTable();  

  <span class="hljs-function"><span class="hljs-keyword">else</span>  <span class="hljs-title">if</span>  (<span class="hljs-params">(f = tabAt(tab, i = (n -  <span class="hljs-number">1</span>    </span>) &amp; hash)) </span>==  <span class="hljs-keyword">null</span>    ) {    <span class="hljs-comment">//&#x5982;&#x679C;i&#x4F4D;&#x7F6E;&#x6CA1;&#x6709;&#x6570;&#x636E;&#xFF0C;&#x5C31;&#x76F4;&#x63A5;&#x65E0;&#x9501;&#x63D2;&#x5165;  </span>

  <span class="hljs-keyword">if</span>  (casTabAt(tab, i,  <span class="hljs-keyword">null</span>    ,  

  <span class="hljs-keyword">new</span>  Node&lt;K,V&gt;(hash, key, <span class="hljs-keyword">value</span>,  <span class="hljs-keyword">null</span>    )))  

  <span class="hljs-keyword">break</span>    ;  <span class="hljs-comment">// no lock when adding to empty bin  </span>

  }  

  <span class="hljs-function"><span class="hljs-keyword">else</span>  <span class="hljs-title">if</span>  (<span class="hljs-params">(fh = f.hash</span>) </span>== MOVED)    <span class="hljs-comment">//&#x5982;&#x679C;&#x5728;&#x8FDB;&#x884C;&#x6269;&#x5BB9;&#xFF0C;&#x5219;&#x5148;&#x8FDB;&#x884C;&#x6269;&#x5BB9;&#x64CD;&#x4F5C;  </span>

  tab = helpTransfer(tab, f);  

  <span class="hljs-keyword">else</span>  {  

  V oldVal =  <span class="hljs-keyword">null</span>    ;  

  <span class="hljs-comment">//&#x5982;&#x679C;&#x4EE5;&#x4E0A;&#x6761;&#x4EF6;&#x90FD;&#x4E0D;&#x6EE1;&#x8DB3;&#xFF0C;&#x90A3;&#x5C31;&#x8981;&#x8FDB;&#x884C;&#x52A0;&#x9501;&#x64CD;&#x4F5C;&#xFF0C;&#x4E5F;&#x5C31;&#x662F;&#x5B58;&#x5728;hash&#x51B2;&#x7A81;&#xFF0C;&#x9501;&#x4F4F;&#x94FE;&#x8868;&#x6216;&#x8005;&#x7EA2;&#x9ED1;&#x6811;&#x7684;&#x5934;&#x7ED3;&#x70B9;  </span>

  synchronized  (f) {  

  <span class="hljs-keyword">if</span>  (tabAt(tab, i) == f) {  

  <span class="hljs-keyword">if</span>  (fh &gt;=  <span class="hljs-number">0</span>    ) {  <span class="hljs-comment">//&#x8868;&#x793A;&#x8BE5;&#x8282;&#x70B9;&#x662F;&#x94FE;&#x8868;&#x7ED3;&#x6784;  </span>

  binCount =  <span class="hljs-number">1</span>    ;  

  <span class="hljs-keyword">for</span>  (Node&lt;K,V&gt; e = f;; ++binCount) {  

  K ek;  

  <span class="hljs-comment">//&#x8FD9;&#x91CC;&#x6D89;&#x53CA;&#x5230;&#x76F8;&#x540C;&#x7684;key&#x8FDB;&#x884C;put&#x5C31;&#x4F1A;&#x8986;&#x76D6;&#x539F;&#x5148;&#x7684;value  </span>

  <span class="hljs-keyword">if</span>  (e.hash == hash &amp;&amp;  

  ((ek = e.key) == key ||  

  (ek !=  <span class="hljs-keyword">null</span>  &amp;&amp; key.equals(ek)))) {  

  oldVal = e.val;  

  <span class="hljs-keyword">if</span>  (!onlyIfAbsent)  

  e.val = <span class="hljs-keyword">value</span>;  

  <span class="hljs-keyword">break</span>    ;  

  }  

  Node&lt;K,V&gt; pred = e;  

  <span class="hljs-keyword">if</span>  ((e = e.next) ==  <span class="hljs-keyword">null</span>    ) {  <span class="hljs-comment">//&#x63D2;&#x5165;&#x94FE;&#x8868;&#x5C3E;&#x90E8;  </span>

  pred.next =  <span class="hljs-keyword">new</span>  Node&lt;K,V&gt;(hash, key,  

  <span class="hljs-keyword">value</span>,  <span class="hljs-keyword">null</span>    );  

  <span class="hljs-keyword">break</span>    ;  

  }  

  }  

  }  

  <span class="hljs-function"><span class="hljs-keyword">else</span>  <span class="hljs-title">if</span>  (<span class="hljs-params">f  instanceof  TreeBin</span>) </span>{    <span class="hljs-comment">//&#x7EA2;&#x9ED1;&#x6811;&#x7ED3;&#x6784;  </span>

  Node&lt;K,V&gt; p;  

  binCount =  <span class="hljs-number">2</span>    ;  

  <span class="hljs-comment">//&#x7EA2;&#x9ED1;&#x6811;&#x7ED3;&#x6784;&#x65CB;&#x8F6C;&#x63D2;&#x5165;  </span>

  <span class="hljs-keyword">if</span>  ((p = ((TreeBin&lt;K,V&gt;)f).putTreeVal(hash, key,  

  <span class="hljs-keyword">value</span>)) !=  <span class="hljs-keyword">null</span>    ) {  

  oldVal = p.val;  

  <span class="hljs-keyword">if</span>  (!onlyIfAbsent)  

  p.val = <span class="hljs-keyword">value</span>;  

  }  

  }  

  }  

  }  

  <span class="hljs-keyword">if</span>  (binCount !=  <span class="hljs-number">0</span>    ) {  <span class="hljs-comment">//&#x5982;&#x679C;&#x94FE;&#x8868;&#x7684;&#x957F;&#x5EA6;&#x5927;&#x4E8E;8&#x65F6;&#x5C31;&#x4F1A;&#x8FDB;&#x884C;&#x7EA2;&#x9ED1;&#x6811;&#x7684;&#x8F6C;&#x6362;  </span>

  <span class="hljs-keyword">if</span>  (binCount &gt;= TREEIFY_THRESHOLD)  

  treeifyBin(tab, i);  

  <span class="hljs-keyword">if</span>  (oldVal !=  <span class="hljs-keyword">null</span>    )  

  <span class="hljs-keyword">return</span>  oldVal;  

  <span class="hljs-keyword">break</span>    ;  

  }  

  }  

  }  

  addCount(<span class="hljs-number">1</span>L, binCount);    <span class="hljs-comment">//&#x7EDF;&#x8BA1;size&#xFF0C;&#x5E76;&#x4E14;&#x68C0;&#x67E5;&#x662F;&#x5426;&#x9700;&#x8981;&#x6269;&#x5BB9;  </span>

  <span class="hljs-keyword">return</span>  <span class="hljs-keyword">null</span>    ;  

  }
</code></pre>
<p>&#x8FD9;&#x4E2A;put&#x7684;&#x8FC7;&#x7A0B;&#x5F88;&#x6E05;&#x6670;&#xFF0C;&#x5BF9;&#x5F53;&#x524D;&#x7684;table&#x8FDB;&#x884C;&#x65E0;&#x6761;&#x4EF6;&#x81EA;&#x5FAA;&#x73AF;&#x76F4;&#x5230;put&#x6210;&#x529F;&#xFF0C;&#x53EF;&#x4EE5;&#x5206;&#x6210;&#x4EE5;&#x4E0B;&#x516D;&#x6B65;&#x6D41;&#x7A0B;&#x6765;&#x6982;&#x8FF0;</p>
<blockquote>
<ol>
<li>&#x5982;&#x679C;&#x6CA1;&#x6709;&#x521D;&#x59CB;&#x5316;&#x5C31;&#x5148;&#x8C03;&#x7528;initTable&#xFF08;&#xFF09;&#x65B9;&#x6CD5;&#x6765;&#x8FDB;&#x884C;&#x521D;&#x59CB;&#x5316;&#x8FC7;&#x7A0B;</li>
<li>&#x5982;&#x679C;&#x6CA1;&#x6709;hash&#x51B2;&#x7A81;&#x5C31;&#x76F4;&#x63A5;CAS&#x63D2;&#x5165;</li>
<li>&#x5982;&#x679C;&#x8FD8;&#x5728;&#x8FDB;&#x884C;&#x6269;&#x5BB9;&#x64CD;&#x4F5C;&#x5C31;&#x5148;&#x8FDB;&#x884C;&#x6269;&#x5BB9;</li>
<li>&#x5982;&#x679C;&#x5B58;&#x5728;hash&#x51B2;&#x7A81;&#xFF0C;&#x5C31;&#x52A0;&#x9501;&#x6765;&#x4FDD;&#x8BC1;&#x7EBF;&#x7A0B;&#x5B89;&#x5168;&#xFF0C;&#x8FD9;&#x91CC;&#x6709;&#x4E24;&#x79CD;&#x60C5;&#x51B5;&#xFF0C;&#x4E00;&#x79CD;&#x662F;&#x94FE;&#x8868;&#x5F62;&#x5F0F;&#x5C31;&#x76F4;&#x63A5;&#x904D;&#x5386;&#x5230;&#x5C3E;&#x7AEF;&#x63D2;&#x5165;&#xFF0C;&#x4E00;&#x79CD;&#x662F;&#x7EA2;&#x9ED1;&#x6811;&#x5C31;&#x6309;&#x7167;&#x7EA2;&#x9ED1;&#x6811;&#x7ED3;&#x6784;&#x63D2;&#x5165;&#xFF0C;</li>
<li>&#x6700;&#x540E;&#x4E00;&#x4E2A;&#x5982;&#x679C;Hash&#x51B2;&#x7A81;&#x65F6;&#x4F1A;&#x5F62;&#x6210;Node&#x94FE;&#x8868;&#xFF0C;&#x5728;&#x94FE;&#x8868;&#x957F;&#x5EA6;&#x8D85;&#x8FC7;8&#xFF0C;Node&#x6570;&#x7EC4;&#x8D85;&#x8FC7;64&#x65F6;&#x4F1A;&#x5C06;&#x94FE;&#x8868;&#x7ED3;&#x6784;&#x8F6C;&#x6362;&#x4E3A;&#x7EA2;&#x9ED1;&#x6811;&#x7684;&#x7ED3;&#x6784;&#xFF0C;break&#x518D;&#x4E00;&#x6B21;&#x8FDB;&#x5165;&#x5FAA;&#x73AF;</li>
<li>&#x5982;&#x679C;&#x6DFB;&#x52A0;&#x6210;&#x529F;&#x5C31;&#x8C03;&#x7528;addCount&#xFF08;&#xFF09;&#x65B9;&#x6CD5;&#x7EDF;&#x8BA1;size&#xFF0C;&#x5E76;&#x4E14;&#x68C0;&#x67E5;&#x662F;&#x5426;&#x9700;&#x8981;&#x6269;&#x5BB9;</li>
</ol>
</blockquote>
<p>&#x73B0;&#x5728;&#x6211;&#x4EEC;&#x6765;&#x5BF9;&#x6BCF;&#x4E00;&#x6B65;&#x7684;&#x7EC6;&#x8282;&#x8FDB;&#x884C;&#x6E90;&#x7801;&#x5206;&#x6790;&#xFF0C;&#x5728;&#x7B2C;&#x4E00;&#x6B65;&#x4E2D;&#xFF0C;&#x7B26;&#x5408;&#x6761;&#x4EF6;&#x4F1A;&#x8FDB;&#x884C;&#x521D;&#x59CB;&#x5316;&#x64CD;&#x4F5C;&#xFF0C;&#x6211;&#x4EEC;&#x6765;&#x770B;&#x770B;initTable&#xFF08;&#xFF09;&#x65B9;&#x6CD5;</p>
<pre><code class="lang-csharp">  <span class="hljs-comment">/**  

  * Initializes table, using the size recorded in sizeCtl.  

  */</span>  

  <span class="hljs-keyword">private</span>  final  Node&lt;K,V&gt;[] initTable() {  

  Node&lt;K,V&gt;[] tab;  <span class="hljs-keyword">int</span>  sc;  

  <span class="hljs-keyword">while</span>  ((tab = table) ==  <span class="hljs-keyword">null</span>  || tab.length ==  <span class="hljs-number">0</span>    ) {    <span class="hljs-comment">//&#x7A7A;&#x7684;table&#x624D;&#x80FD;&#x8FDB;&#x5165;&#x521D;&#x59CB;&#x5316;&#x64CD;&#x4F5C;  </span>

  <span class="hljs-keyword">if</span>  ((sc = sizeCtl) &lt;  <span class="hljs-number">0</span>    )  <span class="hljs-comment">//sizeCtl&lt;0&#x8868;&#x793A;&#x5176;&#x4ED6;&#x7EBF;&#x7A0B;&#x5DF2;&#x7ECF;&#x5728;&#x521D;&#x59CB;&#x5316;&#x4E86;&#x6216;&#x8005;&#x6269;&#x5BB9;&#x4E86;&#xFF0C;&#x6302;&#x8D77;&#x5F53;&#x524D;&#x7EBF;&#x7A0B;  </span>

  Thread.<span class="hljs-keyword">yield</span>();  <span class="hljs-comment">// lost initialization race; just spin  </span>

  <span class="hljs-function"><span class="hljs-keyword">else</span>  <span class="hljs-title">if</span>  (<span class="hljs-params">U.compareAndSwapInt(    <span class="hljs-keyword">this</span>    , SIZECTL, sc, -    <span class="hljs-number">1</span>    </span>)) </span>{    <span class="hljs-comment">//CAS&#x64CD;&#x4F5C;SIZECTL&#x4E3A;-1&#xFF0C;&#x8868;&#x793A;&#x521D;&#x59CB;&#x5316;&#x72B6;&#x6001;  </span>

  <span class="hljs-keyword">try</span>  {  

  <span class="hljs-keyword">if</span>  ((tab = table) ==  <span class="hljs-keyword">null</span>  || tab.length ==  <span class="hljs-number">0</span>    ) {  

  <span class="hljs-keyword">int</span>  n = (sc &gt;  <span class="hljs-number">0</span>    ) ? sc : DEFAULT_CAPACITY;  

  @SuppressWarnings    (    <span class="hljs-string">&quot;unchecked&quot;</span>    )  

  Node&lt;K,V&gt;[] nt = (Node&lt;K,V&gt;[])    <span class="hljs-keyword">new</span>  Node&lt;?,?&gt;[n];    <span class="hljs-comment">//&#x521D;&#x59CB;&#x5316;  </span>

  table = tab = nt;  

  sc = n - (n &gt;&gt;&gt;  <span class="hljs-number">2</span>    );    <span class="hljs-comment">//&#x8BB0;&#x5F55;&#x4E0B;&#x6B21;&#x6269;&#x5BB9;&#x7684;&#x5927;&#x5C0F;  </span>

  }  

  }  <span class="hljs-keyword">finally</span>  {  

  sizeCtl = sc;  

  }  

  <span class="hljs-keyword">break</span>    ;  

  }  

  }  

  <span class="hljs-keyword">return</span>  tab;  

  }
</code></pre>
<p>&#x5728;&#x7B2C;&#x4E8C;&#x6B65;&#x4E2D;&#x6CA1;&#x6709;hash&#x51B2;&#x7A81;&#x5C31;&#x76F4;&#x63A5;&#x8C03;&#x7528;Unsafe&#x7684;&#x65B9;&#x6CD5;CAS&#x63D2;&#x5165;&#x8BE5;&#x5143;&#x7D20;&#xFF0C;&#x8FDB;&#x5165;&#x7B2C;&#x4E09;&#x6B65;&#x5982;&#x679C;&#x5BB9;&#x5668;&#x6B63;&#x5728;&#x6269;&#x5BB9;&#xFF0C;&#x5219;&#x4F1A;&#x8C03;&#x7528;helpTransfer&#xFF08;&#xFF09;&#x65B9;&#x6CD5;&#x5E2E;&#x52A9;&#x6269;&#x5BB9;&#xFF0C;&#x73B0;&#x5728;&#x6211;&#x4EEC;&#x8DDF;&#x8FDB;helpTransfer&#xFF08;&#xFF09;&#x65B9;&#x6CD5;&#x770B;&#x770B;</p>
<pre><code class="lang-kotlin">  <span class="hljs-comment">/**  

  *&#x5E2E;&#x52A9;&#x4ECE;&#x65E7;&#x7684;table&#x7684;&#x5143;&#x7D20;&#x590D;&#x5236;&#x5230;&#x65B0;&#x7684;table&#x4E2D;  

  */</span>  

  <span class="hljs-keyword">final</span>  Node<span class="hljs-type">&lt;K,V&gt;</span>[] helpTransfer(Node<span class="hljs-type">&lt;K,V&gt;</span>[] tab, Node<span class="hljs-type">&lt;K,V&gt;</span> f) {  

  Node<span class="hljs-type">&lt;K,V&gt;</span>[] nextTab;  int  sc;  

  <span class="hljs-keyword">if</span>  (tab !=  <span class="hljs-literal">null</span>  &amp;&amp; (f  instanceof  ForwardingNode) &amp;&amp;  

  (nextTab = ((ForwardingNode<span class="hljs-type">&lt;K,V&gt;</span>)f).nextTable) !=  <span class="hljs-literal">null</span>    ) {  <span class="hljs-comment">//&#x65B0;&#x7684;table nextTba&#x5DF2;&#x7ECF;&#x5B58;&#x5728;&#x524D;&#x63D0;&#x4E0B;&#x624D;&#x80FD;&#x5E2E;&#x52A9;&#x6269;&#x5BB9;  </span>

  int  rs = resizeStamp(tab.length);  

  <span class="hljs-keyword">while</span>  (nextTab == nextTable &amp;&amp; table == tab &amp;&amp;  

  (sc = sizeCtl) <span class="hljs-type">&lt;  0    ) {  

  if  ((sc &gt;</span>&gt;&gt; RESIZE_STAMP_SHIFT) != rs || sc == rs +  <span class="hljs-number">1</span>  ||  

  sc == rs + MAX_RESIZERS || transferIndex <span class="hljs-type">&lt;=  0    )  

  break    ;  

  if  (U.compareAndSwapInt(    this    , SIZECTL, sc, sc +  1    )) {  

  transfer(tab, nextTab);    //&#x8C03;&#x7528;&#x6269;&#x5BB9;&#x65B9;&#x6CD5;  

  break    ;  

  }  

  }  

  return  nextTab;  

  }  

  return  table;  

  }
</span></code></pre>
<p>&#x5176;&#x5B9E;helpTransfer&#xFF08;&#xFF09;&#x65B9;&#x6CD5;&#x7684;&#x76EE;&#x7684;&#x5C31;&#x662F;&#x8C03;&#x7528;&#x591A;&#x4E2A;&#x5DE5;&#x4F5C;&#x7EBF;&#x7A0B;&#x4E00;&#x8D77;&#x5E2E;&#x52A9;&#x8FDB;&#x884C;&#x6269;&#x5BB9;&#xFF0C;&#x8FD9;&#x6837;&#x7684;&#x6548;&#x7387;&#x5C31;&#x4F1A;&#x66F4;&#x9AD8;&#xFF0C;&#x800C;&#x4E0D;&#x662F;&#x53EA;&#x6709;&#x68C0;&#x67E5;&#x5230;&#x8981;&#x6269;&#x5BB9;&#x7684;&#x90A3;&#x4E2A;&#x7EBF;&#x7A0B;&#x8FDB;&#x884C;&#x6269;&#x5BB9;&#x64CD;&#x4F5C;&#xFF0C;&#x5176;&#x4ED6;&#x7EBF;&#x7A0B;&#x5C31;&#x8981;&#x7B49;&#x5F85;&#x6269;&#x5BB9;&#x64CD;&#x4F5C;&#x5B8C;&#x6210;&#x624D;&#x80FD;&#x5DE5;&#x4F5C;
 &#x65E2;&#x7136;&#x8FD9;&#x91CC;&#x6D89;&#x53CA;&#x5230;&#x6269;&#x5BB9;&#x7684;&#x64CD;&#x4F5C;&#xFF0C;&#x6211;&#x4EEC;&#x4E5F;&#x4E00;&#x8D77;&#x6765;&#x770B;&#x770B;&#x6269;&#x5BB9;&#x65B9;&#x6CD5;transfer&#xFF08;&#xFF09;</p>
<pre><code class="lang-java">  <span class="hljs-function"><span class="hljs-keyword">private</span>  <span class="hljs-keyword">final</span>  <span class="hljs-keyword">void</span>  <span class="hljs-title">transfer</span><span class="hljs-params">(Node&lt;K,V&gt;[] tab, Node&lt;K,V&gt;[] nextTab)</span> </span>{  

  <span class="hljs-keyword">int</span>  n = tab.length, stride;  

  <span class="hljs-comment">// &#x6BCF;&#x6838;&#x5904;&#x7406;&#x7684;&#x91CF;&#x5C0F;&#x4E8E;16&#xFF0C;&#x5219;&#x5F3A;&#x5236;&#x8D4B;&#x503C;16  </span>

  <span class="hljs-keyword">if</span>  ((stride = (NCPU &gt;  <span class="hljs-number">1</span>    ) ? (n &gt;&gt;&gt;  <span class="hljs-number">3</span>    ) / NCPU : n) &lt; MIN_TRANSFER_STRIDE)  

  stride = MIN_TRANSFER_STRIDE;  <span class="hljs-comment">// subdivide range  </span>

  <span class="hljs-keyword">if</span>  (nextTab ==  <span class="hljs-keyword">null</span>    ) {  <span class="hljs-comment">// initiating  </span>

  <span class="hljs-keyword">try</span>  {  

  <span class="hljs-meta">@SuppressWarnings</span>    (    <span class="hljs-string">&quot;unchecked&quot;</span>    )  

  Node&lt;K,V&gt;[] nt = (Node&lt;K,V&gt;[])    <span class="hljs-keyword">new</span>  Node&lt;?,?&gt;[n &lt;&lt;  <span class="hljs-number">1</span>    ];  <span class="hljs-comment">//&#x6784;&#x5EFA;&#x4E00;&#x4E2A;nextTable&#x5BF9;&#x8C61;&#xFF0C;&#x5176;&#x5BB9;&#x91CF;&#x4E3A;&#x539F;&#x6765;&#x5BB9;&#x91CF;&#x7684;&#x4E24;&#x500D;  </span>

  nextTab = nt;  

  }  <span class="hljs-keyword">catch</span>  (Throwable ex) {  <span class="hljs-comment">// try to cope with OOME  </span>

  sizeCtl = Integer.MAX_VALUE;  

  <span class="hljs-keyword">return</span>    ;  

  }  

  nextTable = nextTab;  

  transferIndex = n;  

  }  

  <span class="hljs-keyword">int</span>  nextn = nextTab.length;  

  <span class="hljs-comment">// &#x8FDE;&#x63A5;&#x70B9;&#x6307;&#x9488;&#xFF0C;&#x7528;&#x4E8E;&#x6807;&#x5FD7;&#x4F4D;&#xFF08;fwd&#x7684;hash&#x503C;&#x4E3A;-1&#xFF0C;fwd.nextTable=nextTab&#xFF09;  </span>

  ForwardingNode&lt;K,V&gt; fwd =  <span class="hljs-keyword">new</span>  ForwardingNode&lt;K,V&gt;(nextTab);  

  <span class="hljs-comment">// &#x5F53;advance == true&#x65F6;&#xFF0C;&#x8868;&#x660E;&#x8BE5;&#x8282;&#x70B9;&#x5DF2;&#x7ECF;&#x5904;&#x7406;&#x8FC7;&#x4E86;  </span>

  <span class="hljs-keyword">boolean</span>  advance =  <span class="hljs-keyword">true</span>    ;  

  <span class="hljs-keyword">boolean</span>  finishing =  <span class="hljs-keyword">false</span>    ;  <span class="hljs-comment">// to ensure sweep before committing nextTab  </span>

  <span class="hljs-keyword">for</span>  (    <span class="hljs-keyword">int</span>  i =  <span class="hljs-number">0</span>    , bound =  <span class="hljs-number">0</span>    ;;) {  

  Node&lt;K,V&gt; f;  <span class="hljs-keyword">int</span>  fh;  

  <span class="hljs-comment">// &#x63A7;&#x5236; --i ,&#x904D;&#x5386;&#x539F;hash&#x8868;&#x4E2D;&#x7684;&#x8282;&#x70B9;  </span>

  <span class="hljs-keyword">while</span>  (advance) {  

  <span class="hljs-keyword">int</span>  nextIndex, nextBound;  

  <span class="hljs-keyword">if</span>  (--i &gt;= bound || finishing)  

  advance =  <span class="hljs-keyword">false</span>    ;  

  <span class="hljs-keyword">else</span>  <span class="hljs-keyword">if</span>  ((nextIndex = transferIndex) &lt;=  <span class="hljs-number">0</span>    ) {  

  i = -    <span class="hljs-number">1</span>    ;  

  advance =  <span class="hljs-keyword">false</span>    ;  

  }  

  <span class="hljs-comment">// &#x7528;CAS&#x8BA1;&#x7B97;&#x5F97;&#x5230;&#x7684;transferIndex  </span>

  <span class="hljs-keyword">else</span>  <span class="hljs-keyword">if</span>  (U.compareAndSwapInt  

  (    <span class="hljs-keyword">this</span>    , TRANSFERINDEX, nextIndex,  

  nextBound = (nextIndex &gt; stride ?  

  nextIndex - stride :  <span class="hljs-number">0</span>    ))) {  

  bound = nextBound;  

  i = nextIndex -  <span class="hljs-number">1</span>    ;  

  advance =  <span class="hljs-keyword">false</span>    ;  

  }  

  }  

  <span class="hljs-keyword">if</span>  (i &lt;  <span class="hljs-number">0</span>  || i &gt;= n || i + n &gt;= nextn) {  

  <span class="hljs-keyword">int</span>  sc;  

  <span class="hljs-comment">// &#x5DF2;&#x7ECF;&#x5B8C;&#x6210;&#x6240;&#x6709;&#x8282;&#x70B9;&#x590D;&#x5236;&#x4E86;  </span>

  <span class="hljs-keyword">if</span>  (finishing) {  

  nextTable =  <span class="hljs-keyword">null</span>    ;  

  table = nextTab;  <span class="hljs-comment">// table &#x6307;&#x5411;nextTable  </span>

  sizeCtl = (n &lt;&lt;  <span class="hljs-number">1</span>    ) - (n &gt;&gt;&gt;  <span class="hljs-number">1</span>    );  <span class="hljs-comment">// sizeCtl&#x9608;&#x503C;&#x4E3A;&#x539F;&#x6765;&#x7684;1.5&#x500D;  </span>

  <span class="hljs-keyword">return</span>    ;  <span class="hljs-comment">// &#x8DF3;&#x51FA;&#x6B7B;&#x5FAA;&#x73AF;&#xFF0C;  </span>

  }  

  <span class="hljs-comment">// CAS &#x66F4;&#x6269;&#x5BB9;&#x9608;&#x503C;&#xFF0C;&#x5728;&#x8FD9;&#x91CC;&#x9762;sizectl&#x503C;&#x51CF;&#x4E00;&#xFF0C;&#x8BF4;&#x660E;&#x65B0;&#x52A0;&#x5165;&#x4E00;&#x4E2A;&#x7EBF;&#x7A0B;&#x53C2;&#x4E0E;&#x5230;&#x6269;&#x5BB9;&#x64CD;&#x4F5C;  </span>

  <span class="hljs-keyword">if</span>  (U.compareAndSwapInt(    <span class="hljs-keyword">this</span>    , SIZECTL, sc = sizeCtl, sc -  <span class="hljs-number">1</span>    )) {  

  <span class="hljs-keyword">if</span>  ((sc -  <span class="hljs-number">2</span>    ) != resizeStamp(n) &lt;&lt; RESIZE_STAMP_SHIFT)  

  <span class="hljs-keyword">return</span>    ;  

  finishing = advance =  <span class="hljs-keyword">true</span>    ;  

  i = n;  <span class="hljs-comment">// recheck before commit  </span>

  }  

  }  

  <span class="hljs-comment">// &#x904D;&#x5386;&#x7684;&#x8282;&#x70B9;&#x4E3A;null&#xFF0C;&#x5219;&#x653E;&#x5165;&#x5230;ForwardingNode &#x6307;&#x9488;&#x8282;&#x70B9;  </span>

  <span class="hljs-keyword">else</span>  <span class="hljs-keyword">if</span>  ((f = tabAt(tab, i)) ==  <span class="hljs-keyword">null</span>    )  

  advance = casTabAt(tab, i,  <span class="hljs-keyword">null</span>    , fwd);  

  <span class="hljs-comment">// f.hash == -1 &#x8868;&#x793A;&#x904D;&#x5386;&#x5230;&#x4E86;ForwardingNode&#x8282;&#x70B9;&#xFF0C;&#x610F;&#x5473;&#x7740;&#x8BE5;&#x8282;&#x70B9;&#x5DF2;&#x7ECF;&#x5904;&#x7406;&#x8FC7;&#x4E86;  </span>

  <span class="hljs-comment">// &#x8FD9;&#x91CC;&#x662F;&#x63A7;&#x5236;&#x5E76;&#x53D1;&#x6269;&#x5BB9;&#x7684;&#x6838;&#x5FC3;  </span>

  <span class="hljs-keyword">else</span>  <span class="hljs-keyword">if</span>  ((fh = f.hash) == MOVED)  

  advance =  <span class="hljs-keyword">true</span>    ;  <span class="hljs-comment">// already processed  </span>

  <span class="hljs-keyword">else</span>  {  

  <span class="hljs-comment">// &#x8282;&#x70B9;&#x52A0;&#x9501;  </span>

  <span class="hljs-keyword">synchronized</span>  (f) {  

  <span class="hljs-comment">// &#x8282;&#x70B9;&#x590D;&#x5236;&#x5DE5;&#x4F5C;  </span>

  <span class="hljs-keyword">if</span>  (tabAt(tab, i) == f) {  

  Node&lt;K,V&gt; ln, hn;  

  <span class="hljs-comment">// fh &gt;= 0 ,&#x8868;&#x793A;&#x4E3A;&#x94FE;&#x8868;&#x8282;&#x70B9;  </span>

  <span class="hljs-keyword">if</span>  (fh &gt;=  <span class="hljs-number">0</span>    ) {  

  <span class="hljs-comment">// &#x6784;&#x9020;&#x4E24;&#x4E2A;&#x94FE;&#x8868;  &#x4E00;&#x4E2A;&#x662F;&#x539F;&#x94FE;&#x8868;  &#x53E6;&#x4E00;&#x4E2A;&#x662F;&#x539F;&#x94FE;&#x8868;&#x7684;&#x53CD;&#x5E8F;&#x6392;&#x5217;  </span>

  <span class="hljs-keyword">int</span>  runBit = fh &amp; n;  

  Node&lt;K,V&gt; lastRun = f;  

  <span class="hljs-keyword">for</span>  (Node&lt;K,V&gt; p = f.next; p !=  <span class="hljs-keyword">null</span>    ; p = p.next) {  

  <span class="hljs-keyword">int</span>  b = p.hash &amp; n;  

  <span class="hljs-keyword">if</span>  (b != runBit) {  

  runBit = b;  

  lastRun = p;  

  }  

  }  

  <span class="hljs-keyword">if</span>  (runBit ==  <span class="hljs-number">0</span>    ) {  

  ln = lastRun;  

  hn =  <span class="hljs-keyword">null</span>    ;  

  }  

  <span class="hljs-keyword">else</span>  {  

  hn = lastRun;  

  ln =  <span class="hljs-keyword">null</span>    ;  

  }  

  <span class="hljs-keyword">for</span>  (Node&lt;K,V&gt; p = f; p != lastRun; p = p.next) {  

  <span class="hljs-keyword">int</span>  ph = p.hash; K pk = p.key; V pv = p.val;  

  <span class="hljs-keyword">if</span>  ((ph &amp; n) ==  <span class="hljs-number">0</span>    )  

  ln =  <span class="hljs-keyword">new</span>  Node&lt;K,V&gt;(ph, pk, pv, ln);  

  <span class="hljs-keyword">else</span>  

  hn =  <span class="hljs-keyword">new</span>  Node&lt;K,V&gt;(ph, pk, pv, hn);  

  }  

  <span class="hljs-comment">// &#x5728;nextTable i &#x4F4D;&#x7F6E;&#x5904;&#x63D2;&#x4E0A;&#x94FE;&#x8868;  </span>

  setTabAt(nextTab, i, ln);  

  <span class="hljs-comment">// &#x5728;nextTable i + n &#x4F4D;&#x7F6E;&#x5904;&#x63D2;&#x4E0A;&#x94FE;&#x8868;  </span>

  setTabAt(nextTab, i + n, hn);  

  <span class="hljs-comment">// &#x5728;table i &#x4F4D;&#x7F6E;&#x5904;&#x63D2;&#x4E0A;ForwardingNode &#x8868;&#x793A;&#x8BE5;&#x8282;&#x70B9;&#x5DF2;&#x7ECF;&#x5904;&#x7406;&#x8FC7;&#x4E86;  </span>

  setTabAt(tab, i, fwd);  

  <span class="hljs-comment">// advance = true &#x53EF;&#x4EE5;&#x6267;&#x884C;--i&#x52A8;&#x4F5C;&#xFF0C;&#x904D;&#x5386;&#x8282;&#x70B9;  </span>

  advance =  <span class="hljs-keyword">true</span>    ;  

  }  

  <span class="hljs-comment">// &#x5982;&#x679C;&#x662F;TreeBin&#xFF0C;&#x5219;&#x6309;&#x7167;&#x7EA2;&#x9ED1;&#x6811;&#x8FDB;&#x884C;&#x5904;&#x7406;&#xFF0C;&#x5904;&#x7406;&#x903B;&#x8F91;&#x4E0E;&#x4E0A;&#x9762;&#x4E00;&#x81F4;  </span>

  <span class="hljs-keyword">else</span>  <span class="hljs-keyword">if</span>  (f  <span class="hljs-keyword">instanceof</span>  TreeBin) {  

  TreeBin&lt;K,V&gt; t = (TreeBin&lt;K,V&gt;)f;  

  TreeNode&lt;K,V&gt; lo =  <span class="hljs-keyword">null</span>    , loTail =  <span class="hljs-keyword">null</span>    ;  

  TreeNode&lt;K,V&gt; hi =  <span class="hljs-keyword">null</span>    , hiTail =  <span class="hljs-keyword">null</span>    ;  

  <span class="hljs-keyword">int</span>  lc =  <span class="hljs-number">0</span>    , hc =  <span class="hljs-number">0</span>    ;  

  <span class="hljs-keyword">for</span>  (Node&lt;K,V&gt; e = t.first; e !=  <span class="hljs-keyword">null</span>    ; e = e.next) {  

  <span class="hljs-keyword">int</span>  h = e.hash;  

  TreeNode&lt;K,V&gt; p =  <span class="hljs-keyword">new</span>  TreeNode&lt;K,V&gt;  

  (h, e.key, e.val,  <span class="hljs-keyword">null</span>    ,  <span class="hljs-keyword">null</span>    );  

  <span class="hljs-keyword">if</span>  ((h &amp; n) ==  <span class="hljs-number">0</span>    ) {  

  <span class="hljs-keyword">if</span>  ((p.prev = loTail) ==  <span class="hljs-keyword">null</span>    )  

  lo = p;  

  <span class="hljs-keyword">else</span>  

  loTail.next = p;  

  loTail = p;  

  ++lc;  

  }  

  <span class="hljs-keyword">else</span>  {  

  <span class="hljs-keyword">if</span>  ((p.prev = hiTail) ==  <span class="hljs-keyword">null</span>    )  

  hi = p;  

  <span class="hljs-keyword">else</span>  

  hiTail.next = p;  

  hiTail = p;  

  ++hc;  

  }  

  }  

  <span class="hljs-comment">// &#x6269;&#x5BB9;&#x540E;&#x6811;&#x8282;&#x70B9;&#x4E2A;&#x6570;&#x82E5;&lt;=6&#xFF0C;&#x5C06;&#x6811;&#x8F6C;&#x94FE;&#x8868;  </span>

  ln = (lc &lt;= UNTREEIFY_THRESHOLD) ? untreeify(lo) :  

  (hc !=  <span class="hljs-number">0</span>    ) ?  <span class="hljs-keyword">new</span>  TreeBin&lt;K,V&gt;(lo) : t;  

  hn = (hc &lt;= UNTREEIFY_THRESHOLD) ? untreeify(hi) :  

  (lc !=  <span class="hljs-number">0</span>    ) ?  <span class="hljs-keyword">new</span>  TreeBin&lt;K,V&gt;(hi) : t;  

  setTabAt(nextTab, i, ln);  

  setTabAt(nextTab, i + n, hn);  

  setTabAt(tab, i, fwd);  

  advance =  <span class="hljs-keyword">true</span>    ;  

  }  

  }  

  }  

  }  

  }  

  }
</code></pre>
<p>&#x5176;&#x5B9E;helpTransfer&#xFF08;&#xFF09;&#x65B9;&#x6CD5;&#x7684;&#x76EE;&#x7684;&#x5C31;&#x662F;&#x8C03;&#x7528;&#x591A;&#x4E2A;&#x5DE5;&#x4F5C;&#x7EBF;&#x7A0B;&#x4E00;&#x8D77;&#x5E2E;&#x52A9;&#x8FDB;&#x884C;&#x6269;&#x5BB9;&#xFF0C;&#x8FD9;&#x6837;&#x7684;&#x6548;&#x7387;&#x5C31;&#x4F1A;&#x66F4;&#x9AD8;&#xFF0C;&#x800C;&#x4E0D;&#x662F;&#x53EA;&#x6709;&#x68C0;&#x67E5;&#x5230;&#x8981;&#x6269;&#x5BB9;&#x7684;&#x90A3;&#x4E2A;&#x7EBF;&#x7A0B;&#x8FDB;&#x884C;&#x6269;&#x5BB9;&#x64CD;&#x4F5C;&#xFF0C;&#x5176;&#x4ED6;&#x7EBF;&#x7A0B;&#x5C31;&#x8981;&#x7B49;&#x5F85;&#x6269;&#x5BB9;&#x64CD;&#x4F5C;&#x5B8C;&#x6210;&#x624D;&#x80FD;&#x5DE5;&#x4F5C;
 &#x65E2;&#x7136;&#x8FD9;&#x91CC;&#x6D89;&#x53CA;&#x5230;&#x6269;&#x5BB9;&#x7684;&#x64CD;&#x4F5C;&#xFF0C;&#x6211;&#x4EEC;&#x4E5F;&#x4E00;&#x8D77;&#x6765;&#x770B;&#x770B;&#x6269;&#x5BB9;&#x65B9;&#x6CD5;transfer&#xFF08;&#xFF09;</p>
<pre><code class="lang-java">  <span class="hljs-function"><span class="hljs-keyword">private</span>  <span class="hljs-keyword">final</span>  <span class="hljs-keyword">void</span>  <span class="hljs-title">transfer</span><span class="hljs-params">(Node&lt;K,V&gt;[] tab, Node&lt;K,V&gt;[] nextTab)</span> </span>{  

  <span class="hljs-keyword">int</span>  n = tab.length, stride;  

  <span class="hljs-comment">// &#x6BCF;&#x6838;&#x5904;&#x7406;&#x7684;&#x91CF;&#x5C0F;&#x4E8E;16&#xFF0C;&#x5219;&#x5F3A;&#x5236;&#x8D4B;&#x503C;16  </span>

  <span class="hljs-keyword">if</span>  ((stride = (NCPU &gt;  <span class="hljs-number">1</span>    ) ? (n &gt;&gt;&gt;  <span class="hljs-number">3</span>    ) / NCPU : n) &lt; MIN_TRANSFER_STRIDE)  

  stride = MIN_TRANSFER_STRIDE;  <span class="hljs-comment">// subdivide range  </span>

  <span class="hljs-keyword">if</span>  (nextTab ==  <span class="hljs-keyword">null</span>    ) {  <span class="hljs-comment">// initiating  </span>

  <span class="hljs-keyword">try</span>  {  

  <span class="hljs-meta">@SuppressWarnings</span>    (    <span class="hljs-string">&quot;unchecked&quot;</span>    )  

  Node&lt;K,V&gt;[] nt = (Node&lt;K,V&gt;[])    <span class="hljs-keyword">new</span>  Node&lt;?,?&gt;[n &lt;&lt;  <span class="hljs-number">1</span>    ];  <span class="hljs-comment">//&#x6784;&#x5EFA;&#x4E00;&#x4E2A;nextTable&#x5BF9;&#x8C61;&#xFF0C;&#x5176;&#x5BB9;&#x91CF;&#x4E3A;&#x539F;&#x6765;&#x5BB9;&#x91CF;&#x7684;&#x4E24;&#x500D;  </span>

  nextTab = nt;  

  }  <span class="hljs-keyword">catch</span>  (Throwable ex) {  <span class="hljs-comment">// try to cope with OOME  </span>

  sizeCtl = Integer.MAX_VALUE;  

  <span class="hljs-keyword">return</span>    ;  

  }  

  nextTable = nextTab;  

  transferIndex = n;  

  }  

  <span class="hljs-keyword">int</span>  nextn = nextTab.length;  

  <span class="hljs-comment">// &#x8FDE;&#x63A5;&#x70B9;&#x6307;&#x9488;&#xFF0C;&#x7528;&#x4E8E;&#x6807;&#x5FD7;&#x4F4D;&#xFF08;fwd&#x7684;hash&#x503C;&#x4E3A;-1&#xFF0C;fwd.nextTable=nextTab&#xFF09;  </span>

  ForwardingNode&lt;K,V&gt; fwd =  <span class="hljs-keyword">new</span>  ForwardingNode&lt;K,V&gt;(nextTab);  

  <span class="hljs-comment">// &#x5F53;advance == true&#x65F6;&#xFF0C;&#x8868;&#x660E;&#x8BE5;&#x8282;&#x70B9;&#x5DF2;&#x7ECF;&#x5904;&#x7406;&#x8FC7;&#x4E86;  </span>

  <span class="hljs-keyword">boolean</span>  advance =  <span class="hljs-keyword">true</span>    ;  

  <span class="hljs-keyword">boolean</span>  finishing =  <span class="hljs-keyword">false</span>    ;  <span class="hljs-comment">// to ensure sweep before committing nextTab  </span>

  <span class="hljs-keyword">for</span>  (    <span class="hljs-keyword">int</span>  i =  <span class="hljs-number">0</span>    , bound =  <span class="hljs-number">0</span>    ;;) {  

  Node&lt;K,V&gt; f;  <span class="hljs-keyword">int</span>  fh;  

  <span class="hljs-comment">// &#x63A7;&#x5236; --i ,&#x904D;&#x5386;&#x539F;hash&#x8868;&#x4E2D;&#x7684;&#x8282;&#x70B9;  </span>

  <span class="hljs-keyword">while</span>  (advance) {  

  <span class="hljs-keyword">int</span>  nextIndex, nextBound;  

  <span class="hljs-keyword">if</span>  (--i &gt;= bound || finishing)  

  advance =  <span class="hljs-keyword">false</span>    ;  

  <span class="hljs-keyword">else</span>  <span class="hljs-keyword">if</span>  ((nextIndex = transferIndex) &lt;=  <span class="hljs-number">0</span>    ) {  

  i = -    <span class="hljs-number">1</span>    ;  

  advance =  <span class="hljs-keyword">false</span>    ;  

  }  

  <span class="hljs-comment">// &#x7528;CAS&#x8BA1;&#x7B97;&#x5F97;&#x5230;&#x7684;transferIndex  </span>

  <span class="hljs-keyword">else</span>  <span class="hljs-keyword">if</span>  (U.compareAndSwapInt  

  (    <span class="hljs-keyword">this</span>    , TRANSFERINDEX, nextIndex,  

  nextBound = (nextIndex &gt; stride ?  

  nextIndex - stride :  <span class="hljs-number">0</span>    ))) {  

  bound = nextBound;  

  i = nextIndex -  <span class="hljs-number">1</span>    ;  

  advance =  <span class="hljs-keyword">false</span>    ;  

  }  

  }  

  <span class="hljs-keyword">if</span>  (i &lt;  <span class="hljs-number">0</span>  || i &gt;= n || i + n &gt;= nextn) {  

  <span class="hljs-keyword">int</span>  sc;  

  <span class="hljs-comment">// &#x5DF2;&#x7ECF;&#x5B8C;&#x6210;&#x6240;&#x6709;&#x8282;&#x70B9;&#x590D;&#x5236;&#x4E86;  </span>

  <span class="hljs-keyword">if</span>  (finishing) {  

  nextTable =  <span class="hljs-keyword">null</span>    ;  

  table = nextTab;  <span class="hljs-comment">// table &#x6307;&#x5411;nextTable  </span>

  sizeCtl = (n &lt;&lt;  <span class="hljs-number">1</span>    ) - (n &gt;&gt;&gt;  <span class="hljs-number">1</span>    );  <span class="hljs-comment">// sizeCtl&#x9608;&#x503C;&#x4E3A;&#x539F;&#x6765;&#x7684;1.5&#x500D;  </span>

  <span class="hljs-keyword">return</span>    ;  <span class="hljs-comment">// &#x8DF3;&#x51FA;&#x6B7B;&#x5FAA;&#x73AF;&#xFF0C;  </span>

  }  

  <span class="hljs-comment">// CAS &#x66F4;&#x6269;&#x5BB9;&#x9608;&#x503C;&#xFF0C;&#x5728;&#x8FD9;&#x91CC;&#x9762;sizectl&#x503C;&#x51CF;&#x4E00;&#xFF0C;&#x8BF4;&#x660E;&#x65B0;&#x52A0;&#x5165;&#x4E00;&#x4E2A;&#x7EBF;&#x7A0B;&#x53C2;&#x4E0E;&#x5230;&#x6269;&#x5BB9;&#x64CD;&#x4F5C;  </span>

  <span class="hljs-keyword">if</span>  (U.compareAndSwapInt(    <span class="hljs-keyword">this</span>    , SIZECTL, sc = sizeCtl, sc -  <span class="hljs-number">1</span>    )) {  

  <span class="hljs-keyword">if</span>  ((sc -  <span class="hljs-number">2</span>    ) != resizeStamp(n) &lt;&lt; RESIZE_STAMP_SHIFT)  

  <span class="hljs-keyword">return</span>    ;  

  finishing = advance =  <span class="hljs-keyword">true</span>    ;  

  i = n;  <span class="hljs-comment">// recheck before commit  </span>

  }  

  }  

  <span class="hljs-comment">// &#x904D;&#x5386;&#x7684;&#x8282;&#x70B9;&#x4E3A;null&#xFF0C;&#x5219;&#x653E;&#x5165;&#x5230;ForwardingNode &#x6307;&#x9488;&#x8282;&#x70B9;  </span>

  <span class="hljs-keyword">else</span>  <span class="hljs-keyword">if</span>  ((f = tabAt(tab, i)) ==  <span class="hljs-keyword">null</span>    )  

  advance = casTabAt(tab, i,  <span class="hljs-keyword">null</span>    , fwd);  

  <span class="hljs-comment">// f.hash == -1 &#x8868;&#x793A;&#x904D;&#x5386;&#x5230;&#x4E86;ForwardingNode&#x8282;&#x70B9;&#xFF0C;&#x610F;&#x5473;&#x7740;&#x8BE5;&#x8282;&#x70B9;&#x5DF2;&#x7ECF;&#x5904;&#x7406;&#x8FC7;&#x4E86;  </span>

  <span class="hljs-comment">// &#x8FD9;&#x91CC;&#x662F;&#x63A7;&#x5236;&#x5E76;&#x53D1;&#x6269;&#x5BB9;&#x7684;&#x6838;&#x5FC3;  </span>

  <span class="hljs-keyword">else</span>  <span class="hljs-keyword">if</span>  ((fh = f.hash) == MOVED)  

  advance =  <span class="hljs-keyword">true</span>    ;  <span class="hljs-comment">// already processed  </span>

  <span class="hljs-keyword">else</span>  {  

  <span class="hljs-comment">// &#x8282;&#x70B9;&#x52A0;&#x9501;  </span>

  <span class="hljs-keyword">synchronized</span>  (f) {  

  <span class="hljs-comment">// &#x8282;&#x70B9;&#x590D;&#x5236;&#x5DE5;&#x4F5C;  </span>

  <span class="hljs-keyword">if</span>  (tabAt(tab, i) == f) {  

  Node&lt;K,V&gt; ln, hn;  

  <span class="hljs-comment">// fh &gt;= 0 ,&#x8868;&#x793A;&#x4E3A;&#x94FE;&#x8868;&#x8282;&#x70B9;  </span>

  <span class="hljs-keyword">if</span>  (fh &gt;=  <span class="hljs-number">0</span>    ) {  

  <span class="hljs-comment">// &#x6784;&#x9020;&#x4E24;&#x4E2A;&#x94FE;&#x8868;  &#x4E00;&#x4E2A;&#x662F;&#x539F;&#x94FE;&#x8868;  &#x53E6;&#x4E00;&#x4E2A;&#x662F;&#x539F;&#x94FE;&#x8868;&#x7684;&#x53CD;&#x5E8F;&#x6392;&#x5217;  </span>

  <span class="hljs-keyword">int</span>  runBit = fh &amp; n;  

  Node&lt;K,V&gt; lastRun = f;  

  <span class="hljs-keyword">for</span>  (Node&lt;K,V&gt; p = f.next; p !=  <span class="hljs-keyword">null</span>    ; p = p.next) {  

  <span class="hljs-keyword">int</span>  b = p.hash &amp; n;  

  <span class="hljs-keyword">if</span>  (b != runBit) {  

  runBit = b;  

  lastRun = p;  

  }  

  }  

  <span class="hljs-keyword">if</span>  (runBit ==  <span class="hljs-number">0</span>    ) {  

  ln = lastRun;  

  hn =  <span class="hljs-keyword">null</span>    ;  

  }  

  <span class="hljs-keyword">else</span>  {  

  hn = lastRun;  

  ln =  <span class="hljs-keyword">null</span>    ;  

  }  

  <span class="hljs-keyword">for</span>  (Node&lt;K,V&gt; p = f; p != lastRun; p = p.next) {  

  <span class="hljs-keyword">int</span>  ph = p.hash; K pk = p.key; V pv = p.val;  

  <span class="hljs-keyword">if</span>  ((ph &amp; n) ==  <span class="hljs-number">0</span>    )  

  ln =  <span class="hljs-keyword">new</span>  Node&lt;K,V&gt;(ph, pk, pv, ln);  

  <span class="hljs-keyword">else</span>  

  hn =  <span class="hljs-keyword">new</span>  Node&lt;K,V&gt;(ph, pk, pv, hn);  

  }  

  <span class="hljs-comment">// &#x5728;nextTable i &#x4F4D;&#x7F6E;&#x5904;&#x63D2;&#x4E0A;&#x94FE;&#x8868;  </span>

  setTabAt(nextTab, i, ln);  

  <span class="hljs-comment">// &#x5728;nextTable i + n &#x4F4D;&#x7F6E;&#x5904;&#x63D2;&#x4E0A;&#x94FE;&#x8868;  </span>

  setTabAt(nextTab, i + n, hn);  

  <span class="hljs-comment">// &#x5728;table i &#x4F4D;&#x7F6E;&#x5904;&#x63D2;&#x4E0A;ForwardingNode &#x8868;&#x793A;&#x8BE5;&#x8282;&#x70B9;&#x5DF2;&#x7ECF;&#x5904;&#x7406;&#x8FC7;&#x4E86;  </span>

  setTabAt(tab, i, fwd);  

  <span class="hljs-comment">// advance = true &#x53EF;&#x4EE5;&#x6267;&#x884C;--i&#x52A8;&#x4F5C;&#xFF0C;&#x904D;&#x5386;&#x8282;&#x70B9;  </span>

  advance =  <span class="hljs-keyword">true</span>    ;  

  }  

  <span class="hljs-comment">// &#x5982;&#x679C;&#x662F;TreeBin&#xFF0C;&#x5219;&#x6309;&#x7167;&#x7EA2;&#x9ED1;&#x6811;&#x8FDB;&#x884C;&#x5904;&#x7406;&#xFF0C;&#x5904;&#x7406;&#x903B;&#x8F91;&#x4E0E;&#x4E0A;&#x9762;&#x4E00;&#x81F4;  </span>

  <span class="hljs-keyword">else</span>  <span class="hljs-keyword">if</span>  (f  <span class="hljs-keyword">instanceof</span>  TreeBin) {  

  TreeBin&lt;K,V&gt; t = (TreeBin&lt;K,V&gt;)f;  

  TreeNode&lt;K,V&gt; lo =  <span class="hljs-keyword">null</span>    , loTail =  <span class="hljs-keyword">null</span>    ;  

  TreeNode&lt;K,V&gt; hi =  <span class="hljs-keyword">null</span>    , hiTail =  <span class="hljs-keyword">null</span>    ;  

  <span class="hljs-keyword">int</span>  lc =  <span class="hljs-number">0</span>    , hc =  <span class="hljs-number">0</span>    ;  

  <span class="hljs-keyword">for</span>  (Node&lt;K,V&gt; e = t.first; e !=  <span class="hljs-keyword">null</span>    ; e = e.next) {  

  <span class="hljs-keyword">int</span>  h = e.hash;  

  TreeNode&lt;K,V&gt; p =  <span class="hljs-keyword">new</span>  TreeNode&lt;K,V&gt;  

  (h, e.key, e.val,  <span class="hljs-keyword">null</span>    ,  <span class="hljs-keyword">null</span>    );  

  <span class="hljs-keyword">if</span>  ((h &amp; n) ==  <span class="hljs-number">0</span>    ) {  

  <span class="hljs-keyword">if</span>  ((p.prev = loTail) ==  <span class="hljs-keyword">null</span>    )  

  lo = p;  

  <span class="hljs-keyword">else</span>  

  loTail.next = p;  

  loTail = p;  

  ++lc;  

  }  

  <span class="hljs-keyword">else</span>  {  

  <span class="hljs-keyword">if</span>  ((p.prev = hiTail) ==  <span class="hljs-keyword">null</span>    )  

  hi = p;  

  <span class="hljs-keyword">else</span>  

  hiTail.next = p;  

  hiTail = p;  

  ++hc;  

  }  

  }  

  <span class="hljs-comment">// &#x6269;&#x5BB9;&#x540E;&#x6811;&#x8282;&#x70B9;&#x4E2A;&#x6570;&#x82E5;&lt;=6&#xFF0C;&#x5C06;&#x6811;&#x8F6C;&#x94FE;&#x8868;  </span>

  ln = (lc &lt;= UNTREEIFY_THRESHOLD) ? untreeify(lo) :  

  (hc !=  <span class="hljs-number">0</span>    ) ?  <span class="hljs-keyword">new</span>  TreeBin&lt;K,V&gt;(lo) : t;  

  hn = (hc &lt;= UNTREEIFY_THRESHOLD) ? untreeify(hi) :  

  (lc !=  <span class="hljs-number">0</span>    ) ?  <span class="hljs-keyword">new</span>  TreeBin&lt;K,V&gt;(hi) : t;  

  setTabAt(nextTab, i, ln);  

  setTabAt(nextTab, i + n, hn);  

  setTabAt(tab, i, fwd);  

  advance =  <span class="hljs-keyword">true</span>    ;  

  }  

  }  

  }  

  }  

  }  

  }
</code></pre>
<p>&#x6269;&#x5BB9;&#x8FC7;&#x7A0B;&#x6709;&#x70B9;&#x590D;&#x6742;&#xFF0C;&#x8FD9;&#x91CC;&#x4E3B;&#x8981;&#x6D89;&#x53CA;&#x5230;&#x591A;&#x7EBF;&#x7A0B;&#x5E76;&#x53D1;&#x6269;&#x5BB9;,ForwardingNode&#x7684;&#x4F5C;&#x7528;&#x5C31;&#x662F;&#x652F;&#x6301;&#x6269;&#x5BB9;&#x64CD;&#x4F5C;&#xFF0C;&#x5C06;&#x5DF2;&#x5904;&#x7406;&#x7684;&#x8282;&#x70B9;&#x548C;&#x7A7A;&#x8282;&#x70B9;&#x7F6E;&#x4E3A;ForwardingNode&#xFF0C;&#x5E76;&#x53D1;&#x5904;&#x7406;&#x65F6;&#x591A;&#x4E2A;&#x7EBF;&#x7A0B;&#x7ECF;&#x8FC7;ForwardingNode&#x5C31;&#x8868;&#x793A;&#x5DF2;&#x7ECF;&#x904D;&#x5386;&#x4E86;&#xFF0C;&#x5C31;&#x5F80;&#x540E;&#x904D;&#x5386;&#xFF0C;&#x4E0B;&#x56FE;&#x662F;&#x591A;&#x7EBF;&#x7A0B;&#x5408;&#x4F5C;&#x6269;&#x5BB9;&#x7684;&#x8FC7;&#x7A0B;&#xFF1A;</p>
<p><img src="https://gitee.com/zisuu/picture/raw/master/img/20201222151146.png" alt="image-20201222151146461"></p>
<p>image</p>
<p>&#x4ECB;&#x7ECD;&#x5B8C;&#x6269;&#x5BB9;&#x8FC7;&#x7A0B;&#xFF0C;&#x6211;&#x4EEC;&#x518D;&#x6B21;&#x56DE;&#x5230;put&#x6D41;&#x7A0B;&#xFF0C;&#x5728;&#x7B2C;&#x56DB;&#x6B65;&#x4E2D;&#x662F;&#x5411;&#x94FE;&#x8868;&#x6216;&#x8005;&#x7EA2;&#x9ED1;&#x6811;&#x91CC;&#x52A0;&#x8282;&#x70B9;&#xFF0C;&#x5230;&#x7B2C;&#x4E94;&#x6B65;&#xFF0C;&#x4F1A;&#x8C03;&#x7528;treeifyBin&#xFF08;&#xFF09;&#x65B9;&#x6CD5;&#x8FDB;&#x884C;&#x94FE;&#x8868;&#x8F6C;&#x7EA2;&#x9ED1;&#x6811;&#x7684;&#x8FC7;&#x7A0B;</p>
<pre><code class="lang-java">  <span class="hljs-function"><span class="hljs-keyword">private</span>  <span class="hljs-keyword">final</span>  <span class="hljs-keyword">void</span>  <span class="hljs-title">treeifyBin</span><span class="hljs-params">(Node&lt;K,V&gt;[] tab,  <span class="hljs-keyword">int</span>  index)</span> </span>{  

  Node&lt;K,V&gt; b;  <span class="hljs-keyword">int</span>  n, sc;  

  <span class="hljs-keyword">if</span>  (tab !=  <span class="hljs-keyword">null</span>    ) {  

  <span class="hljs-comment">//&#x5982;&#x679C;&#x6574;&#x4E2A;table&#x7684;&#x6570;&#x91CF;&#x5C0F;&#x4E8E;64&#xFF0C;&#x5C31;&#x6269;&#x5BB9;&#x81F3;&#x539F;&#x6765;&#x7684;&#x4E00;&#x500D;&#xFF0C;&#x4E0D;&#x8F6C;&#x7EA2;&#x9ED1;&#x6811;&#x4E86;  </span>

  <span class="hljs-comment">//&#x56E0;&#x4E3A;&#x8FD9;&#x4E2A;&#x9608;&#x503C;&#x6269;&#x5BB9;&#x53EF;&#x4EE5;&#x51CF;&#x5C11;hash&#x51B2;&#x7A81;&#xFF0C;&#x4E0D;&#x5FC5;&#x8981;&#x53BB;&#x8F6C;&#x7EA2;&#x9ED1;&#x6811;  </span>

  <span class="hljs-keyword">if</span>  ((n = tab.length) &lt; MIN_TREEIFY_CAPACITY)  

  tryPresize(n &lt;&lt;  <span class="hljs-number">1</span>    );  

  <span class="hljs-keyword">else</span>  <span class="hljs-keyword">if</span>  ((b = tabAt(tab, index)) !=  <span class="hljs-keyword">null</span>  &amp;&amp; b.hash &gt;=  <span class="hljs-number">0</span>    ) {  

  <span class="hljs-keyword">synchronized</span>  (b) {  

  <span class="hljs-keyword">if</span>  (tabAt(tab, index) == b) {  

  TreeNode&lt;K,V&gt; hd =  <span class="hljs-keyword">null</span>    , tl =  <span class="hljs-keyword">null</span>    ;  

  <span class="hljs-keyword">for</span>  (Node&lt;K,V&gt; e = b; e !=  <span class="hljs-keyword">null</span>    ; e = e.next) {  

  <span class="hljs-comment">//&#x5C01;&#x88C5;&#x6210;TreeNode  </span>

  TreeNode&lt;K,V&gt; p =  

  <span class="hljs-keyword">new</span>  TreeNode&lt;K,V&gt;(e.hash, e.key, e.val,  

  <span class="hljs-keyword">null</span>    ,  <span class="hljs-keyword">null</span>    );  

  <span class="hljs-keyword">if</span>  ((p.prev = tl) ==  <span class="hljs-keyword">null</span>    )  

  hd = p;  

  <span class="hljs-keyword">else</span>  

  tl.next = p;  

  tl = p;  

  }  

  <span class="hljs-comment">//&#x901A;&#x8FC7;TreeBin&#x5BF9;&#x8C61;&#x5BF9;TreeNode&#x8F6C;&#x6362;&#x6210;&#x7EA2;&#x9ED1;&#x6811;  </span>

  setTabAt(tab, index,  <span class="hljs-keyword">new</span>  TreeBin&lt;K,V&gt;(hd));  

  }  

  }  

  }  

  }  

  }
</code></pre>
<p>&#x5230;&#x7B2C;&#x516D;&#x6B65;&#x8868;&#x793A;&#x5DF2;&#x7ECF;&#x6570;&#x636E;&#x52A0;&#x5165;&#x6210;&#x529F;&#x4E86;&#xFF0C;&#x73B0;&#x5728;&#x8C03;&#x7528;addCount()&#x65B9;&#x6CD5;&#x8BA1;&#x7B97;ConcurrentHashMap&#x7684;size&#xFF0C;&#x5728;&#x539F;&#x6765;&#x7684;&#x57FA;&#x7840;&#x4E0A;&#x52A0;&#x4E00;&#xFF0C;&#x73B0;&#x5728;&#x6765;&#x770B;&#x770B;addCount()&#x65B9;&#x6CD5;</p>
<pre><code class="lang-csharp">  <span class="hljs-function"><span class="hljs-keyword">private</span>  final  <span class="hljs-keyword">void</span>  <span class="hljs-title">addCount</span>(<span class="hljs-params">    <span class="hljs-keyword">long</span>  x,  <span class="hljs-keyword">int</span>  check</span>) </span>{  

  CounterCell[] <span class="hljs-keyword">as</span>;  <span class="hljs-keyword">long</span>  b, s;  

  <span class="hljs-comment">//&#x66F4;&#x65B0;baseCount&#xFF0C;table&#x7684;&#x6570;&#x91CF;&#xFF0C;counterCells&#x8868;&#x793A;&#x5143;&#x7D20;&#x4E2A;&#x6570;&#x7684;&#x53D8;&#x5316;  </span>

  <span class="hljs-keyword">if</span>  ((<span class="hljs-keyword">as</span> = counterCells) !=  <span class="hljs-keyword">null</span>  ||  

  !U.compareAndSwapLong(    <span class="hljs-keyword">this</span>    , BASECOUNT, b = baseCount, s = b + x)) {  

  CounterCell a;  <span class="hljs-keyword">long</span>  v;  <span class="hljs-keyword">int</span>  m;  

  boolean  uncontended =  <span class="hljs-keyword">true</span>    ;  

  <span class="hljs-comment">//&#x5982;&#x679C;&#x591A;&#x4E2A;&#x7EBF;&#x7A0B;&#x90FD;&#x5728;&#x6267;&#x884C;&#xFF0C;&#x5219;CAS&#x5931;&#x8D25;&#xFF0C;&#x6267;&#x884C;fullAddCount&#xFF0C;&#x5168;&#x90E8;&#x52A0;&#x5165;count  </span>

  <span class="hljs-keyword">if</span>  (<span class="hljs-keyword">as</span> ==  <span class="hljs-keyword">null</span>  || (m = <span class="hljs-keyword">as</span>.length -  <span class="hljs-number">1</span>    ) &lt;  <span class="hljs-number">0</span>  ||  

  (a = <span class="hljs-keyword">as</span>[ThreadLocalRandom.getProbe() &amp; m]) ==  <span class="hljs-keyword">null</span>  ||  

  !(uncontended =  

  U.compareAndSwapLong(a, CELLVALUE, v = a.<span class="hljs-keyword">value</span>, v + x))) {  

  fullAddCount(x, uncontended);  

  <span class="hljs-keyword">return</span>    ;  

  }  

  <span class="hljs-keyword">if</span>  (check &lt;=  <span class="hljs-number">1</span>    )  

  <span class="hljs-keyword">return</span>    ;  

  s = sumCount();  

  }  

  <span class="hljs-comment">//check&gt;=0&#x8868;&#x793A;&#x9700;&#x8981;&#x8FDB;&#x884C;&#x6269;&#x5BB9;&#x64CD;&#x4F5C;  </span>

  <span class="hljs-keyword">if</span>  (check &gt;=  <span class="hljs-number">0</span>    ) {  

  Node&lt;K,V&gt;[] tab, nt;  <span class="hljs-keyword">int</span>  n, sc;  

  <span class="hljs-keyword">while</span>  (s &gt;= (    <span class="hljs-keyword">long</span>    )(sc = sizeCtl) &amp;&amp; (tab = table) !=  <span class="hljs-keyword">null</span>  &amp;&amp;  

  (n = tab.length) &lt; MAXIMUM_CAPACITY) {  

  <span class="hljs-keyword">int</span>  rs = resizeStamp(n);  

  <span class="hljs-keyword">if</span>  (sc &lt;  <span class="hljs-number">0</span>    ) {  

  <span class="hljs-keyword">if</span>  ((sc &gt;&gt;&gt; RESIZE_STAMP_SHIFT) != rs || sc == rs +  <span class="hljs-number">1</span>  ||  

  sc == rs + MAX_RESIZERS || (nt = nextTable) ==  <span class="hljs-keyword">null</span>  ||  

  transferIndex &lt;=  <span class="hljs-number">0</span>    )  

  <span class="hljs-keyword">break</span>    ;  

  <span class="hljs-keyword">if</span>  (U.compareAndSwapInt(    <span class="hljs-keyword">this</span>    , SIZECTL, sc, sc +  <span class="hljs-number">1</span>    ))  

  transfer(tab, nt);  

  }  

  <span class="hljs-comment">//&#x5F53;&#x524D;&#x7EBF;&#x7A0B;&#x53D1;&#x8D77;&#x5E93;&#x54E6;&#x54E6;&#x8BA9;&#x64CD;&#x4F5C;&#xFF0C;nextTable=null  </span>

  <span class="hljs-function"><span class="hljs-keyword">else</span>  <span class="hljs-title">if</span>  (<span class="hljs-params">U.compareAndSwapInt(    <span class="hljs-keyword">this</span>    , SIZECTL, sc,  

  (rs &lt;&lt; RESIZE_STAMP_SHIFT</span>) +  2    ))  

  <span class="hljs-title">transfer</span>(<span class="hljs-params">tab,  <span class="hljs-keyword">null</span>    </span>)</span>;  

  s = sumCount();  

  }  

  }  

  }
</code></pre>
<p>put&#x7684;&#x6D41;&#x7A0B;&#x73B0;&#x5728;&#x5DF2;&#x7ECF;&#x5206;&#x6790;&#x5B8C;&#x4E86;&#xFF0C;&#x4F60;&#x53EF;&#x4EE5;&#x4ECE;&#x4E2D;&#x53D1;&#x73B0;&#xFF0C;&#x4ED6;&#x5728;&#x5E76;&#x53D1;&#x5904;&#x7406;&#x4E2D;&#x4F7F;&#x7528;&#x7684;&#x662F;&#x4E50;&#x89C2;&#x9501;&#xFF0C;&#x5F53;&#x6709;&#x51B2;&#x7A81;&#x7684;&#x65F6;&#x5019;&#x624D;&#x8FDB;&#x884C;&#x5E76;&#x53D1;&#x5904;&#x7406;&#xFF0C;&#x800C;&#x4E14;&#x6D41;&#x7A0B;&#x6B65;&#x9AA4;&#x5F88;&#x6E05;&#x6670;&#xFF0C;&#x4F46;&#x662F;&#x7EC6;&#x8282;&#x8BBE;&#x8BA1;&#x7684;&#x5F88;&#x590D;&#x6742;&#xFF0C;&#x6BD5;&#x7ADF;&#x591A;&#x7EBF;&#x7A0B;&#x7684;&#x573A;&#x666F;&#x4E5F;&#x590D;&#x6742;</p>
<h4 id="get&#x64CD;&#x4F5C;_1"><a name="get&#x64CD;&#x4F5C;_1" class="anchor-navigation-ex-anchor" href="#get&#x64CD;&#x4F5C;_1"><i class="fa fa-link" aria-hidden="true"></i></a><a name="get&#x64CD;&#x4F5C;" class="plugin-anchor" href="#get&#x64CD;&#x4F5C;"><i class="fa fa-link" aria-hidden="true"></i></a>get&#x64CD;&#x4F5C;</h4>
<p>&#x6211;&#x4EEC;&#x73B0;&#x5728;&#x8981;&#x56DE;&#x5230;&#x5F00;&#x59CB;&#x7684;&#x4F8B;&#x5B50;&#x4E2D;&#xFF0C;&#x6211;&#x4EEC;&#x5BF9;&#x4E2A;&#x4EBA;&#x4FE1;&#x606F;&#x8FDB;&#x884C;&#x4E86;&#x65B0;&#x589E;&#x4E4B;&#x540E;&#xFF0C;&#x6211;&#x4EEC;&#x8981;&#x83B7;&#x53D6;&#x6240;&#x65B0;&#x589E;&#x7684;&#x4FE1;&#x606F;&#xFF0C;&#x4F7F;&#x7528;String name = map.get(&#x201C;name&#x201D;)&#x83B7;&#x53D6;&#x65B0;&#x589E;&#x7684;name&#x4FE1;&#x606F;&#xFF0C;&#x73B0;&#x5728;&#x6211;&#x4EEC;&#x4F9D;&#x65E7;&#x7528;debug&#x7684;&#x65B9;&#x5F0F;&#x6765;&#x5206;&#x6790;&#x4E0B;ConcurrentHashMap&#x7684;&#x83B7;&#x53D6;&#x65B9;&#x6CD5;get()</p>
<pre><code class="lang-kotlin">  <span class="hljs-keyword">public</span>  V <span class="hljs-keyword">get</span>(Object key) {  

  Node<span class="hljs-type">&lt;K,V&gt;</span>[] tab; Node<span class="hljs-type">&lt;K,V&gt;</span> e, p;  int  n, eh; K ek;  

  int  h = spread(key.hashCode());  <span class="hljs-comment">//&#x8BA1;&#x7B97;&#x4E24;&#x6B21;hash  </span>

  <span class="hljs-keyword">if</span>  ((tab = table) !=  <span class="hljs-literal">null</span>  &amp;&amp; (n = tab.length) &gt;  <span class="hljs-number">0</span>  &amp;&amp;  

  (e = tabAt(tab, (n -  <span class="hljs-number">1</span>    ) &amp; h)) !=  <span class="hljs-literal">null</span>    ) {    <span class="hljs-comment">//&#x8BFB;&#x53D6;&#x9996;&#x8282;&#x70B9;&#x7684;Node&#x5143;&#x7D20;  </span>

  <span class="hljs-keyword">if</span>  ((eh = e.hash) == h) {  <span class="hljs-comment">//&#x5982;&#x679C;&#x8BE5;&#x8282;&#x70B9;&#x5C31;&#x662F;&#x9996;&#x8282;&#x70B9;&#x5C31;&#x8FD4;&#x56DE;  </span>

  <span class="hljs-keyword">if</span>  ((ek = e.key) == key || (ek !=  <span class="hljs-literal">null</span>  &amp;&amp; key.equals(ek)))  

  <span class="hljs-keyword">return</span>  e.<span class="hljs-keyword">val</span>;  

  }  

  <span class="hljs-comment">//hash&#x503C;&#x4E3A;&#x8D1F;&#x503C;&#x8868;&#x793A;&#x6B63;&#x5728;&#x6269;&#x5BB9;&#xFF0C;&#x8FD9;&#x4E2A;&#x65F6;&#x5019;&#x67E5;&#x7684;&#x662F;ForwardingNode&#x7684;find&#x65B9;&#x6CD5;&#x6765;&#x5B9A;&#x4F4D;&#x5230;nextTable&#x6765;  </span>

  <span class="hljs-comment">//&#x67E5;&#x627E;&#xFF0C;&#x67E5;&#x627E;&#x5230;&#x5C31;&#x8FD4;&#x56DE;  </span>

  <span class="hljs-keyword">else</span>  <span class="hljs-keyword">if</span>  (eh <span class="hljs-type">&lt;  0    )  

  return  (p = e.find(h, key)) !=  null  ? p.val :  null    ;  

  while  ((e = e.next) !=  null    ) {    //&#x65E2;&#x4E0D;&#x662F;&#x9996;&#x8282;&#x70B9;&#x4E5F;&#x4E0D;&#x662F;ForwardingNode&#xFF0C;&#x90A3;&#x5C31;&#x5F80;&#x4E0B;&#x904D;&#x5386;  

  if  (e.hash == h &amp;&amp;  

  ((ek = e.key) == key || (ek !=  null  &amp;&amp; key.equals(ek))))  

  return  e.val;  

  }  

  }  

  return  null    ;  

  }
</span></code></pre>
<p>ConcurrentHashMap&#x7684;get&#x64CD;&#x4F5C;&#x7684;&#x6D41;&#x7A0B;&#x5F88;&#x7B80;&#x5355;&#xFF0C;&#x4E5F;&#x5F88;&#x6E05;&#x6670;&#xFF0C;&#x53EF;&#x4EE5;&#x5206;&#x4E3A;&#x4E09;&#x4E2A;&#x6B65;&#x9AA4;&#x6765;&#x63CF;&#x8FF0;</p>
<ol>
<li>&#x8BA1;&#x7B97;hash&#x503C;&#xFF0C;&#x5B9A;&#x4F4D;&#x5230;&#x8BE5;table&#x7D22;&#x5F15;&#x4F4D;&#x7F6E;&#xFF0C;&#x5982;&#x679C;&#x662F;&#x9996;&#x8282;&#x70B9;&#x7B26;&#x5408;&#x5C31;&#x8FD4;&#x56DE;</li>
<li>&#x5982;&#x679C;&#x9047;&#x5230;&#x6269;&#x5BB9;&#x7684;&#x65F6;&#x5019;&#xFF0C;&#x4F1A;&#x8C03;&#x7528;&#x6807;&#x5FD7;&#x6B63;&#x5728;&#x6269;&#x5BB9;&#x8282;&#x70B9;ForwardingNode&#x7684;find&#x65B9;&#x6CD5;&#xFF0C;&#x67E5;&#x627E;&#x8BE5;&#x8282;&#x70B9;&#xFF0C;&#x5339;&#x914D;&#x5C31;&#x8FD4;&#x56DE;</li>
<li>&#x4EE5;&#x4E0A;&#x90FD;&#x4E0D;&#x7B26;&#x5408;&#x7684;&#x8BDD;&#xFF0C;&#x5C31;&#x5F80;&#x4E0B;&#x904D;&#x5386;&#x8282;&#x70B9;&#xFF0C;&#x5339;&#x914D;&#x5C31;&#x8FD4;&#x56DE;&#xFF0C;&#x5426;&#x5219;&#x6700;&#x540E;&#x5C31;&#x8FD4;&#x56DE;null</li>
</ol>
<h4 id="size&#x64CD;&#x4F5C;_1"><a name="size&#x64CD;&#x4F5C;_1" class="anchor-navigation-ex-anchor" href="#size&#x64CD;&#x4F5C;_1"><i class="fa fa-link" aria-hidden="true"></i></a><a name="size&#x64CD;&#x4F5C;" class="plugin-anchor" href="#size&#x64CD;&#x4F5C;"><i class="fa fa-link" aria-hidden="true"></i></a>size&#x64CD;&#x4F5C;</h4>
<p>&#x6700;&#x540E;&#x6211;&#x4EEC;&#x6765;&#x770B;&#x4E0B;&#x4F8B;&#x5B50;&#x4E2D;&#x6700;&#x540E;&#x83B7;&#x53D6;size&#x7684;&#x65B9;&#x5F0F;int size = map.size();&#xFF0C;&#x73B0;&#x5728;&#x8BA9;&#x6211;&#x4EEC;&#x770B;&#x4E0B;size()&#x65B9;&#x6CD5;</p>
<pre><code class="lang-csharp">  <span class="hljs-function"><span class="hljs-keyword">public</span>  <span class="hljs-keyword">int</span>  <span class="hljs-title">size</span>(<span class="hljs-params"></span>) </span>{  

  <span class="hljs-keyword">long</span>  n = sumCount();  

  <span class="hljs-keyword">return</span>  ((n &lt; <span class="hljs-number">0</span>L) ?  <span class="hljs-number">0</span>  :  

  (n &gt; (    <span class="hljs-keyword">long</span>    )Integer.MAX_VALUE) ? Integer.MAX_VALUE :  

  (    <span class="hljs-keyword">int</span>    )n);  

  }  

  <span class="hljs-function">final  <span class="hljs-keyword">long</span>  <span class="hljs-title">sumCount</span>(<span class="hljs-params"></span>) </span>{  

  CounterCell[] <span class="hljs-keyword">as</span> = counterCells; CounterCell a;  <span class="hljs-comment">//&#x53D8;&#x5316;&#x7684;&#x6570;&#x91CF;  </span>

  <span class="hljs-keyword">long</span>  sum = baseCount;  

  <span class="hljs-keyword">if</span>  (<span class="hljs-keyword">as</span> !=  <span class="hljs-keyword">null</span>    ) {  

  <span class="hljs-keyword">for</span>  (    <span class="hljs-keyword">int</span>  i =  <span class="hljs-number">0</span>    ; i &lt; <span class="hljs-keyword">as</span>.length; ++i) {  

  <span class="hljs-keyword">if</span>  ((a = <span class="hljs-keyword">as</span>[i]) !=  <span class="hljs-keyword">null</span>    )  

  sum += a.<span class="hljs-keyword">value</span>;  

  }  

  }  

  <span class="hljs-keyword">return</span>  sum;  

  }
</code></pre>
<p>&#x5728;JDK1.8&#x7248;&#x672C;&#x4E2D;&#xFF0C;&#x5BF9;&#x4E8E;size&#x7684;&#x8BA1;&#x7B97;&#xFF0C;&#x5728;&#x6269;&#x5BB9;&#x548C;addCount()&#x65B9;&#x6CD5;&#x5C31;&#x5DF2;&#x7ECF;&#x6709;&#x5904;&#x7406;&#x4E86;&#xFF0C;JDK1.7&#x662F;&#x5728;&#x8C03;&#x7528;size()&#x65B9;&#x6CD5;&#x624D;&#x53BB;&#x8BA1;&#x7B97;&#xFF0C;&#x5176;&#x5B9E;&#x5728;&#x5E76;&#x53D1;&#x96C6;&#x5408;&#x4E2D;&#x53BB;&#x8BA1;&#x7B97;size&#x662F;&#x6CA1;&#x6709;&#x591A;&#x5927;&#x7684;&#x610F;&#x4E49;&#x7684;&#xFF0C;&#x56E0;&#x4E3A;size&#x662F;&#x5B9E;&#x65F6;&#x5728;&#x53D8;&#x7684;&#xFF0C;&#x53EA;&#x80FD;&#x8BA1;&#x7B97;&#x67D0;&#x4E00;&#x523B;&#x7684;&#x5927;&#x5C0F;&#xFF0C;&#x4F46;&#x662F;&#x67D0;&#x4E00;&#x523B;&#x592A;&#x5FEB;&#x4E86;&#xFF0C;&#x4EBA;&#x7684;&#x611F;&#x77E5;&#x662F;&#x4E00;&#x4E2A;&#x65F6;&#x95F4;&#x6BB5;&#xFF0C;&#x6240;&#x4EE5;&#x5E76;&#x4E0D;&#x662F;&#x5F88;&#x7CBE;&#x786E;</p>
<h2 id="&#x603B;&#x7ED3;&#x4E0E;&#x601D;&#x8003;"><a name="&#x603B;&#x7ED3;&#x4E0E;&#x601D;&#x8003;" class="anchor-navigation-ex-anchor" href="#&#x603B;&#x7ED3;&#x4E0E;&#x601D;&#x8003;"><i class="fa fa-link" aria-hidden="true"></i></a><a name="&#x603B;&#x7ED3;&#x4E0E;&#x601D;&#x8003;" class="plugin-anchor" href="#&#x603B;&#x7ED3;&#x4E0E;&#x601D;&#x8003;"><i class="fa fa-link" aria-hidden="true"></i></a>&#x603B;&#x7ED3;&#x4E0E;&#x601D;&#x8003;</h2>
<p>&#x5176;&#x5B9E;&#x53EF;&#x4EE5;&#x770B;&#x51FA;JDK1.8&#x7248;&#x672C;&#x7684;ConcurrentHashMap&#x7684;&#x6570;&#x636E;&#x7ED3;&#x6784;&#x5DF2;&#x7ECF;&#x63A5;&#x8FD1;HashMap&#xFF0C;&#x76F8;&#x5BF9;&#x800C;&#x8A00;&#xFF0C;ConcurrentHashMap&#x53EA;&#x662F;&#x589E;&#x52A0;&#x4E86;&#x540C;&#x6B65;&#x7684;&#x64CD;&#x4F5C;&#x6765;&#x63A7;&#x5236;&#x5E76;&#x53D1;&#xFF0C;&#x4ECE;JDK1.7&#x7248;&#x672C;&#x7684;ReentrantLock+Segment+HashEntry&#xFF0C;&#x5230;JDK1.8&#x7248;&#x672C;&#x4E2D;synchronized+CAS+HashEntry+&#x7EA2;&#x9ED1;&#x6811;,&#x76F8;&#x5BF9;&#x800C;&#x8A00;&#xFF0C;&#x603B;&#x7ED3;&#x5982;&#x4E0B;&#x601D;&#x8003;</p>
<ol>
<li>JDK1.8&#x7684;&#x5B9E;&#x73B0;&#x964D;&#x4F4E;&#x9501;&#x7684;&#x7C92;&#x5EA6;&#xFF0C;JDK1.7&#x7248;&#x672C;&#x9501;&#x7684;&#x7C92;&#x5EA6;&#x662F;&#x57FA;&#x4E8E;Segment&#x7684;&#xFF0C;&#x5305;&#x542B;&#x591A;&#x4E2A;HashEntry&#xFF0C;&#x800C;JDK1.8&#x9501;&#x7684;&#x7C92;&#x5EA6;&#x5C31;&#x662F;HashEntry&#xFF08;&#x9996;&#x8282;&#x70B9;&#xFF09;</li>
<li>JDK1.8&#x7248;&#x672C;&#x7684;&#x6570;&#x636E;&#x7ED3;&#x6784;&#x53D8;&#x5F97;&#x66F4;&#x52A0;&#x7B80;&#x5355;&#xFF0C;&#x4F7F;&#x5F97;&#x64CD;&#x4F5C;&#x4E5F;&#x66F4;&#x52A0;&#x6E05;&#x6670;&#x6D41;&#x7545;&#xFF0C;&#x56E0;&#x4E3A;&#x5DF2;&#x7ECF;&#x4F7F;&#x7528;synchronized&#x6765;&#x8FDB;&#x884C;&#x540C;&#x6B65;&#xFF0C;&#x6240;&#x4EE5;&#x4E0D;&#x9700;&#x8981;&#x5206;&#x6BB5;&#x9501;&#x7684;&#x6982;&#x5FF5;&#xFF0C;&#x4E5F;&#x5C31;&#x4E0D;&#x9700;&#x8981;Segment&#x8FD9;&#x79CD;&#x6570;&#x636E;&#x7ED3;&#x6784;&#x4E86;&#xFF0C;&#x7531;&#x4E8E;&#x7C92;&#x5EA6;&#x7684;&#x964D;&#x4F4E;&#xFF0C;&#x5B9E;&#x73B0;&#x7684;&#x590D;&#x6742;&#x5EA6;&#x4E5F;&#x589E;&#x52A0;&#x4E86;</li>
<li>JDK1.8&#x4F7F;&#x7528;&#x7EA2;&#x9ED1;&#x6811;&#x6765;&#x4F18;&#x5316;&#x94FE;&#x8868;&#xFF0C;&#x57FA;&#x4E8E;&#x957F;&#x5EA6;&#x5F88;&#x957F;&#x7684;&#x94FE;&#x8868;&#x7684;&#x904D;&#x5386;&#x662F;&#x4E00;&#x4E2A;&#x5F88;&#x6F2B;&#x957F;&#x7684;&#x8FC7;&#x7A0B;&#xFF0C;&#x800C;&#x7EA2;&#x9ED1;&#x6811;&#x7684;&#x904D;&#x5386;&#x6548;&#x7387;&#x662F;&#x5F88;&#x5FEB;&#x7684;&#xFF0C;&#x4EE3;&#x66FF;&#x4E00;&#x5B9A;&#x9608;&#x503C;&#x7684;&#x94FE;&#x8868;&#xFF0C;&#x8FD9;&#x6837;&#x5F62;&#x6210;&#x4E00;&#x4E2A;&#x6700;&#x4F73;&#x62CD;&#x6863;</li>
<li><strong>JDK1.8&#x4E3A;&#x4EC0;&#x4E48;&#x4F7F;&#x7528;&#x5185;&#x7F6E;&#x9501;synchronized&#x6765;&#x4EE3;&#x66FF;&#x91CD;&#x5165;&#x9501;ReentrantLock&#xFF0C;&#x6211;&#x89C9;&#x5F97;&#x6709;&#x4EE5;&#x4E0B;&#x51E0;&#x70B9;</strong><ol>
<li>&#x56E0;&#x4E3A;&#x7C92;&#x5EA6;&#x964D;&#x4F4E;&#x4E86;&#xFF0C;&#x5728;&#x76F8;&#x5BF9;&#x800C;&#x8A00;&#x7684;&#x4F4E;&#x7C92;&#x5EA6;&#x52A0;&#x9501;&#x65B9;&#x5F0F;&#xFF0C;synchronized&#x5E76;&#x4E0D;&#x6BD4;ReentrantLock&#x5DEE;&#xFF0C;&#x5728;&#x7C97;&#x7C92;&#x5EA6;&#x52A0;&#x9501;&#x4E2D;ReentrantLock&#x53EF;&#x80FD;&#x901A;&#x8FC7;Condition&#x6765;&#x63A7;&#x5236;&#x5404;&#x4E2A;&#x4F4E;&#x7C92;&#x5EA6;&#x7684;&#x8FB9;&#x754C;&#xFF0C;&#x66F4;&#x52A0;&#x7684;&#x7075;&#x6D3B;&#xFF0C;&#x800C;&#x5728;&#x4F4E;&#x7C92;&#x5EA6;&#x4E2D;&#xFF0C;Condition&#x7684;&#x4F18;&#x52BF;&#x5C31;&#x6CA1;&#x6709;&#x4E86;</li>
<li>JVM&#x7684;&#x5F00;&#x53D1;&#x56E2;&#x961F;&#x4ECE;&#x6765;&#x90FD;&#x6CA1;&#x6709;&#x653E;&#x5F03;synchronized&#xFF0C;&#x800C;&#x4E14;&#x57FA;&#x4E8E;JVM&#x7684;synchronized&#x4F18;&#x5316;&#x7A7A;&#x95F4;&#x66F4;&#x5927;&#xFF0C;&#x4F7F;&#x7528;&#x5185;&#x5D4C;&#x7684;&#x5173;&#x952E;&#x5B57;&#x6BD4;&#x4F7F;&#x7528;API&#x66F4;&#x52A0;&#x81EA;&#x7136;</li>
<li>&#x5728;&#x5927;&#x91CF;&#x7684;&#x6570;&#x636E;&#x64CD;&#x4F5C;&#x4E0B;&#xFF0C;&#x5BF9;&#x4E8E;JVM&#x7684;&#x5185;&#x5B58;&#x538B;&#x529B;&#xFF0C;&#x57FA;&#x4E8E;API&#x7684;ReentrantLock&#x4F1A;&#x5F00;&#x9500;&#x66F4;&#x591A;&#x7684;&#x5185;&#x5B58;&#xFF0C;&#x867D;&#x7136;&#x4E0D;&#x662F;&#x74F6;&#x9888;&#xFF0C;&#x4F46;&#x662F;&#x4E5F;&#x662F;&#x4E00;&#x4E2A;&#x9009;&#x62E9;&#x4F9D;&#x636E;</li>
</ol>
</li>
</ol>

<script>console.log("plugin-popup....");document.onclick = function(e){ e.target.tagName === "IMG" && window.open(e.target.src,e.target.src)}</script><style>img{cursor:pointer}</style>
                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="2.HashMap源码分析.html" class="navigation navigation-prev navigation-unique" aria-label="Previous page: 2.hashMap底层原理">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"3.ConcurrentHashMap分析","level":"7.3","depth":1,"next":{"title":"1.创造型模式","level":"8.1","depth":1,"ref":"","articles":[]},"previous":{"title":"2.hashMap底层原理","level":"7.2","depth":1,"path":"basic/collection/2.HashMap源码分析.md","ref":"basic/collection/2.HashMap源码分析.md","articles":[]},"dir":"ltr"},"config":{"plugins":["-sharing","splitter","expandable-chapters-small","anchors","sharing-plus","anchor-navigation-ex","favicon","expandable-chapters","toggle-chapters","back-to-top-button","code","-lunr","-search","search-pro","pageview-count","popup","livereload"],"styles":{"website":"./styles/website.css"},"pluginsConfig":{"livereload":{},"splitter":{},"search-pro":{},"sharing-plus":{"qq":false,"all":["facebook","google","twitter","instapaper","linkedin","pocket","stumbleupon"],"douban":false,"facebook":true,"weibo":false,"instapaper":false,"whatsapp":false,"hatenaBookmark":false,"twitter":true,"messenger":false,"line":false,"vk":false,"pocket":true,"google":false,"viber":false,"stumbleupon":false,"qzone":false,"linkedin":false},"popup":{},"code":{"copyButtons":true},"fontsettings":{"theme":"white","family":"sans","size":2},"highlight":{},"anchor-navigation-ex":{"associatedWithSummary":true,"float":{"floatIcon":"fa fa-navicon","level1Icon":"","level2Icon":"","level3Icon":"","showLevelIcon":false},"mode":"float","multipleH1":true,"pageTop":{"level1Icon":"","level2Icon":"","level3Icon":"","showLevelIcon":false},"printLog":false,"showGoTop":true,"showLevel":false},"favicon":{},"back-to-top-button":{},"pageview-count":{},"expandable-chapters-small":{},"sharing":{"qq":false,"all":["google","facebook","weibo","twitter","qq","qzone","linkedin","pocket"],"douban":false,"facebook":false,"weibo":false,"instapaper":false,"whatsapp":false,"hatenaBookmark":false,"twitter":false,"messenger":false,"line":false,"vk":false,"pocket":false,"google":false,"viber":false,"stumbleupon":false,"qzone":false,"linkedin":false},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false},"anchors":{},"toggle-chapters":{},"expandable-chapters":{}},"theme":"default","author":"hzh","pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"variables":{},"title":"计算机 ,java , 大数据 基础知识体系","language":"zh-hans","gitbook":"3.2.3"},"file":{"path":"basic/collection/3.ConcurrentHashMap分析.md","mtime":"2020-12-22T07:12:44.731Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2020-12-30T11:12:56.611Z"},"basePath":"../..","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="../../gitbook/gitbook.js"></script>
    <script src="../../gitbook/theme.js"></script>
    
        
        <script src="../../gitbook/gitbook-plugin-splitter/splitter.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-expandable-chapters-small/expandable-chapters-small.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-sharing-plus/buttons.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-expandable-chapters/expandable-chapters.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-toggle-chapters/toggle.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-back-to-top-button/plugin.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-code/plugin.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-search-pro/jquery.mark.min.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-search-pro/search.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-pageview-count/plugin.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-livereload/plugin.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    </body>
</html>

